如何转换数组中已经存在于 javaScript 的嵌套对象中的对象

how to convert objects in array which is already in a nested 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);

我正在尝试获取宠物小精灵的详细信息,但无法使用上述代码获取 next_evolution 和 pre_evolution 的详细信息 所以代码应该是我必须在警报 window 提示中给出口袋妖怪的名字。 该函数将检查宠物小精灵的名称,它应该提供该宠物小精灵的全部详细信息,包括它是否包含下一次进化和之前的进化细节 谁能帮我解决这个问题...... outPut of the above code

您可以通过以下方式编辑您的函数:

function getPokemonByName(name) {

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

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

return 'Not Found'
}

然后就可以用精灵名字来调用了

getPokemonByName('Ivysaur')

这将给出正确的结果。

您仍然可以通过在 next_evolution 和 pre_evolution 上添加带有 for loopif 语句来使用您的函数,如下所示:

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');