循环不会迭代所有JSON数据集

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环不会迭代所有JSON数据集相关的知识,希望对你有一定的参考价值。

在JSON数据集中,循环只是在第一个口袋妖怪上迭代,即对于Bulbasaur而言是真的,并且循环也在结尾处也使用else语句,这是不需要的。但是如果我在循环之后添加一个中断,那么代码就会崩溃。如果你输入任何其他口袋妖怪的名字,它会显示“未找到”。如果你输入“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] + "
")

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

checkname(findname, findpokemongame)
答案

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

  • 检查第一个元素
  • 它是我们搜索的那个?
  • 如果是 - 提醒'找到'
  • 如果没有 - 未找到警报并完成算法(休息)
  • 检查第二个元素(如果'找到'的是普遍的条件(即我们正在搜索第一个条目))
  • 警告'未找到'并完成算法(休息)

所以你应该重写逻辑,就像这样

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')
}
另一答案

现在你正在迭代所有的口袋妖怪,但如果第一个不匹配你就打破了循环。

相反,只有在迭代通过所有没有匹配的口袋妖怪时才更改if / else语句以显示“Not found”。

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

以上是关于循环不会迭代所有JSON数据集的主要内容,如果未能解决你的问题,请参考以下文章

如何在Django视图中使用for循环返回每次迭代[关闭]

结果集在while循环中仅迭代一次

解决未能加载文件或程序集“Newtonsoft.Json ...."或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)(代码片段

Python - 循环加速 - 大型数据集

Node.js 同步循环或迭代异步语句

不使用循环从可迭代对象中获取值