Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

734
Visualizações
Javascript API Fetch make a filter PokeApi

I´m studying Javascript. And for that I am making a Pokémon Game based on the PokéAPI

To make it simple, I want to retrieve - filter - Pokémons with types Fire, Water & Grass.

enter image description here

I understand I should go to data > types > (look in the slots 1 & 2) > search for grass, fire & water.

So this is my code, so far, and I was thinking if I could do it BEFORE retrieving the data to the API. Otherwise I´ll have to retrieve EVERY Pokémon and then filter them.

const baseUrl = 'https://pokeapi.co/api/v2/pokemon/?limit=150'
try {
    fetch(baseUrl)
        .then(response => {
            const responseJson = response.json()
            return responseJson
        })

    .then(async data => {
        const pokemons = data.results


        for (const pokemon of pokemons) {
            pokemon.data = await fetch(pokemon.url).then(res => res.json())
        }



        console.log(pokemons)
    })

    .catch(error => {
        console.error(error)
    })
} catch (error) {
    console.error(error)
}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you do not normalize the API data you have to filter using this list. But when you get the pokemon list you can normalize the list to a more friendly structure to your app.

function normalizeList (item = {}) {
  const { name, data } = item
   // Define your normalized data here

  return {
    name,
    types: data.types
  }
}

If you need to transform/parse or whatever I recomend you to create a PokemonModel and create Pokemon Class instances:

class PokemonModel {
  constructor(data = {}) {
    this.name = data.name
    this.types = this._getTypes(data.types)
  }

  _getTypes(types = []) {
    return types.map((type) => {
      return {
        name: type.name
      }
    })
  }
}

Your code with PokemonModel:

class PokemonModel {
  constructor(data = {}) {
    this.name = data.name
    this.types = this._getTypes(data.types)
  }

  _getTypes(types = []) {
    return types.map((type) => {
      return {
        name: type.name
      }
    })
  }
}

const baseUrl = 'https://pokeapi.co/api/v2/pokemon/?limit=150'
const pokemonList = []

try {
  fetch(baseUrl)
    .then(response => {
      const responseJson = response.json()
      return responseJson
    })

  .then(async data => {
    const pokemons = data.results

    for (const pokemon of pokemons) {
      pokemon.data = await fetch(pokemon.url).then(res => res.json())
      pokemonList.push(new PokemonModel(pokemon.data))
    }
    console.log(pokemonList)
  })

  .catch(error => {
    console.error(error)
  })
} catch (error) {
  console.error(error)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Of course, you can't filter with data that you haven't yet, but you can save the data in localStorage for the first time and use it next times. You can see examples here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda