I have been trying to create a function with 4 parameters that allows me to push an object in an existing array. However, I am not getting the result I want. For example, this is the existing array:
const firstGen = [
{name: 'Mewtwo', hp: 110, type: 'physic'},
{name: 'Charizard', hp: 135, type: 'flying'},
{name: 'Pikachu', hp: 85, type: 'electric'},
{name: 'Totodile', hp: 55, type: 'water'},
{name: 'Bayleef', hp: 80, type: 'grass'},
{name: 'Typlosion', hp: 125, type: 'fire'}
]
And this is the function I have created:
const addPokemon = (pokedex, pokemonName, pokemonHp, pokemonType) => {
pokedex.push({pokemonName,pokemonHp, pokemonType})
}
Example input:
addPokemon(secondGen, 'Lucario', 85, 'fighting')
But the output is:
{ name: 'Mew', hp: 110, type: 'physic' },
{ name: 'Arcanine', hp: 135, type: 'Fire' },
{ name: 'Raikou', hp: 140, type: 'electric' },
{ name: 'Blastoise', hp: 120, type: 'water' },
{ name: 'Gardevoir', hp: 100, type: 'grass' },
{ name: 'Scorbunny', hp: 45, type: 'fire' },
{ pokemonName: 'Lucario', pokemonHp: 85, pokemonType: 'fighting' }
As you can see, the parameters that I have in my function also appear in the output and not the property names of my array. Thank you in advance.
{name: pokemonName, hp: pokemonHp, type: pokemonType}.{pokemonName,pokemonHp, pokemonType}is shorthand for{pokemonName: pokemonName, pokemonHp: pokemonHp, pokemonType: pokemonType}- there is no way for JS to guess you want different names for these properties, so you just have to do it manually:{name: pokemonName, hp: pokemonHp, type: pokemonType}