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

我正在尝试获取 pokemon 详细信息,但无法使用上述代码获取 next_evolution 和 pre_evolution 详细信息,因此代码应该是我必须在警报窗口提示中提供 pokemon 名称。该函数将检查口袋妖怪的名称,它应该提供该口袋妖怪的全部细节,包括它是否包含下一次进化和以前的进化细节,任何人都可以帮我解决这个问题.... 输出上述代码

4

2 回答 2

0

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

function getPokemonByName(name) {

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

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

return 'Not Found'
}

然后你可以使用口袋妖怪名称来调用它。

getPokemonByName('Ivysaur')

这将给出正确的结果。

在此处输入图像描述

于 2018-07-29T12:59:21.497 回答
0

您仍然可以通过在 next_evolution 和 pre_evolution 上添加if带有 a 的语句来使用您的函数,如下所示: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');

于 2018-07-29T13:25:30.027 回答