簡體   English   中英

如何轉換數組中的對象,該數組已在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的詳細信息,因此代碼應為我必須在警報窗口提示中提供口袋妖怪的名稱。 該功能將檢查口袋妖怪的名稱,並應提供該口袋妖怪的完整詳細信息,包括它是否包含下一個演變過程和以前的詳細信息,任何人都可以幫助我。... 上面代碼的輸出

您可以通過以下方式編輯函數:

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 loop if語句來使用函數,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM