如何转换数组中的对象,该数组已在javaScript中的嵌套对象中

时间:2018-07-29 12:48:50

标签: javascript nested-object

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详细信息 所以代码应该是我必须在警报窗口提示中给出神奇宝贝的名字。 该功能将检查口袋妖怪的名称,并应提供该口袋妖怪的完整详细信息,包括是否包含下一个演变过程和先前的详细信息  有人可以帮我吗...。 outPut of the above code

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式编辑功能:

function getPokemonByName(name) {

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

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

return 'Not Found'
}

然后您可以使用宠物小精灵的名字来称呼它。

getPokemonByName('Ivysaur')

这将给出正确的结果。

enter image description here

答案 1 :(得分:0)

您仍然可以通过在next_evolution和pre_evolution上添加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');