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

301
Visualizações
Trying to combine two arrays of data from pokeapi via an axios.get function in node.js

I'm working on a project right now where I'm accessing pokemon data from PokeAPI with axios. I am only accessing a few points of data, and one of them is the pokemon type. If the pokemon I'm searching for has two types, it will return the types as separate arrays.

For example, if I search Pikachu, it returns with just one array, which is Electric, because pikachu only has one type. However, if I search for Gengar, it will return two arrays. One with poison, and one with ghost.

How can I combine these into a single array of types?

Here is my code :

module.exports = {
  getPokemon: async (req, res) => {
    const { pokemon } = req.body;
    try {
      const { data } = await axios.get(
        `https://pokeapi.co/api/v2/pokemon/${pokemon.toLowerCase()}`
      );
      const types = data.types.map((pokemon) => pokemon.type);
      console.log(types);
      party.push({
        name: data.name,
        sprites: data.sprites.front_default,
        types: data.types,
      });
      res.status(200).send(party);
    } catch (err) {
      console.log(err);
      res.status(500).send("something went wrong");
    }
  },
};

And the response I get in the console :

name: "gengar"
sprites: "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/94.png"
types: Array(2)
0:
slot: 1
type: {name: 'ghost', url: 'https://pokeapi.co/api/v2/type/8/'}
[[Prototype]]: Object
1:
slot: 2
type: {name: 'poison', url: 'https://pokeapi.co/api/v2/type/4/'}
about 4 years ago · Juan Pablo Isaza
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