循环未在所有JSON数据集上迭代

时间:2019-03-24 10:33:19

标签: javascript json

在JSON数据集中的此处,循环仅在第一个宠物小精灵上进行迭代,即仅对Bulbasaur为true,并且循环在末尾也采用了else语句,这不是必需的。但是,如果我在循环之后添加一个break,否则代码将崩溃。如果您键入其他任何宠物小精灵的名字,则显示“未找到”。如果您输入“ Ivysaur”或其他任何神奇宝贝名称(例如“ Venusaur”),则不会显示。在下面查看我的代码。

let findpokemongame = {
  "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"
    }]
  }, {
    "id": 3,
    "num": "003",
    "name": "Venusaur",
    "img": "http://www.serebii.net/pokemongo/pokemon/003.png",
    "type": [
      "Grass",
      "Poison"
    ],
    "height": "2.01 m",
    "weight": "100.0 kg",
    "candy": "Bulbasaur Candy",
    "egg": "Not in Eggs",
    "spawn_chance": 0.017,
    "avg_spawns": 1.7,
    "spawn_time": "11:30",
    "multipliers": null,
    "weaknesses": [
      "Fire",
      "Ice",
      "Flying",
      "Psychic"
    ],
    "prev_evolution": [{
      "num": "001",
      "name": "Bulbasaur"
    }, {
      "num": "002",
      "name": "Ivysaur"
    }]
  }]
};

var findname = window.prompt("Enter Pokemon Name")
let checkname = function(findname, findpokemongame) {
  for (let thispokemon in findpokemongame.pokemon) {
    if (findpokemongame.pokemon[thispokemon].name == findname) {
      let pokemondetails = findpokemongame.pokemon[thispokemon];
      console.log(pokemondetails);
      for (info in pokemondetails) {
        if (typeof pokemondetails[info][0] === 'object') {
          pokemondetails[info] = pokemondetails[info].map(o => o.name)
        }

        alert(info + " : " + pokemondetails[info] + "\n")

      }
    }
    else{
      alert('Not found');
      break;
    }
  }
}

checkname(findname, findpokemongame)

2 个答案:

答案 0 :(得分:0)

您的算法基本上是如何工作的:

  • 检查第一个元素
  • 是我们要找的那个吗?
  • 如果是-提醒“找到”
  • 如果否-提醒“未找到”并完成算法(中断)

  • 检查第二个元素(如果“发现”了previos条件(即我们正在搜索第一个条目))

  • 警告“未找到”并完成算法(中断)

所以您应该像这样重写逻辑

let found = false;

for (....) {
  if (element.name === findname) {  
    found = element;
    break;
  }
}

if (found) {
  console.log('found', element)
} else {
  console.log('not found')
}

但是我更喜欢使用find方式:

const found = array.find(function (element) {
  return element.name === findname
})

if (found) {
  console.log('found', element)
} else {
  console.log('not found')
}

答案 1 :(得分:0)

现在,您正在遍历所有神奇宝贝,但是如果第一个不匹配,则会中断循环。

如果改为,则仅当遍历所有不匹配的宠物小精灵时,才将if / else语句更改为显示“未找到”。

if (findpokemongame.pokemon[thispokemon].name == findname) {
  ...
  // We found a match, display details
  for (info in pokemondetails) {...}
  break; // Break when all the details have been displayed
}
else if (thispokemon == findpokemongame.pokemon.length - 1) {
  alert('Not found'); // We only reach this when having iterated through all pokemon and none matched
}
当我们遍历最后一个宠物小精灵时,

thispokemon等于findpokemongame.pokemon.length - 1