how to get details nested object which is array is nested with object in javaScript

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


how to get details nested object which is array is nested with object in javaScript


var pokemonName = window.prompt("Enter the pokemon details")
var pokemon = [{
"id": 1,
"num": "001",
"name": "Bulbasaur",
"img": "http://www.serebii.net/pokemongo/pokemon/001.png",
"type": [
"Grass",
"Poison"
],
"height": "0.71 m",
"weight": "6.9 kg",
"candy": "Bulbasaur Candy",
"candy_count": 25,
"egg": "2 km",
"spawn_chance": 0.69,
"avg_spawns": 69,
"spawn_time": "20:00",
"multipliers": [1.58],
"weaknesses": [
"Fire",
"Ice",
"Flying",
"Psychic"
],
"next_evolution": [{
"num": "002",
"name": "Ivysaur"
}, {
"num": "003",
"name": "Venusaur"
}]
}, {
"id": 2,
"num": "002",
"name": "Ivysaur",
"img": "http://www.serebii.net/pokemongo/pokemon/002.png",
"type": [
"Grass",
"Poison"
],
"height": "0.99 m",
"weight": "13.0 kg",
"candy": "Bulbasaur Candy",
"candy_count": 100,
"egg": "Not in Eggs",
"spawn_chance": 0.042,
"avg_spawns": 4.2,
"spawn_time": "07:00",
"multipliers": [
1.2,
1.6
],
"weaknesses": [
"Fire",
"Ice",
"Flying",
"Psychic"
],
"prev_evolution": [{
"num": "001",
"name": "Bulbasaur"
}],
"next_evolution": [{
"num": "003",
"name": "Venusaur"
}]
}]

function pokemonDetails(name) {
for (var i = 0; i <= pokemon.length; i++) {
if (name == pokemon[i].name) {
y = pokemon[i]
for (var x in y) {
console.log(x + " = " + y[x] + "n")
}
}
}
}


pokemonDetails(pokemonName);



i am trying get the pokemon detailsout but unable to get the next_evolution and pre_evolution details with the above code
so the code should be i have to give the pokemon name in the alert window prompt.
the function will check the pokemon name and it should give the full detail of that pokemon including with if it contains next evolution and previous evloution details
can anyone please help me with that....
outPut of the above code




2 Answers
2



You can edit your function in following way:


function getPokemonByName(name) {

const pokemonIndex = pokemon.findIndex((item) => item.name === name )

if (pokemonIndex > -1) return pokemon[pokemonIndex]

return 'Not Found'
}



Then you can call it using pokemon name.


getPokemonByName('Ivysaur')



This will give proper result.



enter image description here



You can still use your function by adding if statement with a for loop on the next_evolution and pre_evolution like this:


if


for loop




var pokemon = [{
"id": 1,
"num": "001",
"name": "Bulbasaur",
"img": "http://www.serebii.net/pokemongo/pokemon/001.png",
"type": [
"Grass",
"Poison"
],
"height": "0.71 m",
"weight": "6.9 kg",
"candy": "Bulbasaur Candy",
"candy_count": 25,
"egg": "2 km",
"spawn_chance": 0.69,
"avg_spawns": 69,
"spawn_time": "20:00",
"multipliers": [1.58],
"weaknesses": [
"Fire",
"Ice",
"Flying",
"Psychic"
],
"next_evolution": [{
"num": "002",
"name": "Ivysaur"
}, {
"num": "003",
"name": "Venusaur"
}]
}, {
"id": 2,
"num": "002",
"name": "Ivysaur",
"img": "http://www.serebii.net/pokemongo/pokemon/002.png",
"type": [
"Grass",
"Poison"
],
"height": "0.99 m",
"weight": "13.0 kg",
"candy": "Bulbasaur Candy",
"candy_count": 100,
"egg": "Not in Eggs",
"spawn_chance": 0.042,
"avg_spawns": 4.2,
"spawn_time": "07:00",
"multipliers": [
1.2,
1.6
],
"weaknesses": [
"Fire",
"Ice",
"Flying",
"Psychic"
],
"prev_evolution": [{
"num": "001",
"name": "Bulbasaur"
}],
"next_evolution": [{
"num": "003",
"name": "Venusaur"
}]
}]

function pokemonDetails(name) {
for (var i = 0; i < pokemon.length; i++) {
if (name == pokemon[i].name) {
y = pokemon[i]
for (var x in y) {
if(x === 'next_evolution') {
for (var z = 0; z < y[x].length; z++) {
console.log(x + " = " + "num: " + y[x][z].num + ", name: " + y[x][z].name + "n")
}
}else if (x === 'prev_evolution') {
for (var z = 0; z < y[x].length; z++) {
console.log(x + " = " + "num: " + y[x][z].num + ", name: " + y[x][z].name + "n")
}
} else {
console.log(x + " = " + y[x] + "n")
}

}
}
}
}


pokemonDetails('Ivysaur');






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived