0

I make a request to 'https://pokeapi.co/api/v2/pokemon/${name}' to obtain the information of a specific pokemon, the problem is that the name must be the same otherwise it returns undefined, I need to filter many pokemons, for example: if I search for char it should return charmeleon and charizard, because they both have 'char'. How can I filter a lot of pokemons?

const params = {
  headers: {
    'Content-Type': 'application/json'
  }
}

const searchPokemon = async name => {
  const url = `https://pokeapi.co/api/v2/pokemon/${name}`
  try {
    const response = await fetch(url, params);
    const result = await response.json();
    return result;
  } catch (error) {
    console.log(error)
  }
}

4

1 回答 1

1

要获取所有可能的 pokemon 名称列表,您可以向https://pokeapi.co/api/v2/pokemon?limit=100000(其中 100000 大于存在的 pokemon 数量。目前似乎只有 1118 个 pokemon。)

结果如下所示:

[
  {
    name:"bulbasaur",
    url:"https://pokeapi.co/api/v2/pokemon/1/"
  },
  {
    name:"ivysaur",
    url:"https://pokeapi.co/api/v2/pokemon/2/"
  },
  {
    name:"venusaur",
    url:"https://pokeapi.co/api/v2/pokemon/3/"
  },
  ...
]

然后,您可以根据要查找的名称过滤掉该列表。找到您想要的名称后,您可以使用相应的 URL 获取更多信息。

于 2021-09-10T23:40:47.177 回答