I am trying to write a function that takes pokemon's name as an argument and find out which all pokemon have that name in their “next_evolution” field
Consider the following JSON dataset -
visit https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json
my code for the said question:
var infoOfPokemon = function(nameOfPokemon, allPokemon) {
for (x in allPokemon) {
if (allPokemon[x].next_evolution &&
allPokemon[x].next_evolution.includes(nameOfPokemon)) {
console.log('pokemons found: ' + allPokemon[x].name)
} else {
null
}
}
}
var nameOfPokemon = prompt('enter the name of Pokemon')
infoOfPokemon(nameOfPokemon, pokemonData.pokemon)
my function isn't returning the name of pokemon that has its name in next_evolution field.
In your JSON object, next_evolution object is an array of Objects and it not populated always.
You were operating directly on the next_evolution field considering it as String array hence your include didn't work.
Below code is tedious but gives explanation of your wrong iteration
var infoOfPokemon = function(nameOfPokemon, allPokemon) {
for (index in allPokemon) {
var nextEvolutionObject=allPokemon[index].next_evolution;
if (nextEvolutionObject){
for(evolutionIndex in nextEvolutionObject){
if(nextEvolutionObject[evolutionIndex].name===nameOfPokemon){
console.log("Pokemon found :"+nameOfPokemon);
}
}
}
}
}
var nameOfPokemon = prompt('enter the name of Pokemon');
infoOfPokemon(nameOfPokemon, pokemonData.pokemon)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.