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/'}