无法使用jquery遍历列表

时间:2016-08-28 15:02:20

标签: javascript jquery

我定义了一个像这样的JSON:

\w{7}

所以我想通过这里面的每个元素,我想在 data = [ {"tile1": {"y": 212, "x": 392, "hp": true}, "index": "1", "tile3": {"y": 415, "x": 794, "hp": true}, "tile2": {"y": 415, "x": 793, "hp": true}, "tile5": {"y": 415, "x": 796, "hp": true}, "tile4": {"y": 415, "x": 795, "hp": true}, "tile7": {"y": 416, "x": 792, "hp": true}, "tile6": {"y": 415, "x": 797, "hp": true}, "tile9": {"y": 416, "x": 794, "hp": true}, "tile8": {"y": 416, "x": 793, "hp": true}, "zoom": " 10", "tile11": {"y": 416, "x": 796, "hp": true}, "tile10": {"y": 416, "x": 795, "hp": true}, "tile12": {"y": 416, "x": 797, "hp": true}}, {"tile1": {"y": 416, "x": 792, "hp": true}, "index": "2", "tile3": {"y": 415, "x": 794, "hp": true}, "tile2": {"y": 415, "x": 793, "hp": true}, "tile5": {"y": 415, "x": 796, "hp": true}, "tile4": {"y": 415, "x": 795, "hp": true}, "tile7": {"y": 416, "x": 792, "hp": true}, "tile6": {"y": 415, "x": 797, "hp": true}, "tile9": {"y": 416, "x": 794, "hp": true}, "tile8": {"y": 416, "x": 793, "hp": true}, "zoom": " 10", "tile11": {"y": 416, "x": 796, "hp": true}, "tile10": {"y": 416, "x": 795, "hp": true}, "tile12": {"y": 416, "x": 797, "hp": true}} ]; 的所有元素中的每个项目中得到x和y。 我做了这样的功能:

data

但控制台没有显示任何内容,因为第二个循环根本没有执行?有人可以帮帮我吗?我只需要得到x,y和hp的值。 JSFIDDLE

4 个答案:

答案 0 :(得分:2)

item是一个对象,而不是一个数组。你不能迭代这样的对象。 (严格来说,item.length未定义,因为它不是数组,因此j < item.length始终为false。

相反,迭代对象的传统方法是使用for..in循环:

for(var key in item) {
  if(!item.hasOwnProperty(key)) continue;

  // key is "tile1", "tile2", "tile3", etc.
  var item2 = item[key]; // item2 is now { "x": ..., "y": ..., "hp": ... }
}

答案 1 :(得分:2)

这里的项目是一个不是数组的对象,所以

    for (var j=0; j < item.length; j++) {
        var item2 = item[j]
        console.log("X:" + item[i].x + " Y" + item[j].y);
    } 

取代

function loopRespData(respData){
console.log(respData);
    for (var i=0; i < respData.length; i++) {
        var item = respData[i];
        for(var key in item) {
          if (item.hasOwnProperty(key)) {
            var item2 = item[key];
            if (item2.hasOwnProperty('x')&& item2.hasOwnProperty('y'))
              console.log("X:"+ item2.x + " Y"+ item2.y);
          }
        }
    }
}
var  data = [ {"tile1": {"y": 212, "x": 392, "hp": true}, "index": "1", "tile3": {"y": 415, "x": 794, "hp": true}, "tile2": {"y": 415, "x": 793, "hp": true}, "tile5": {"y": 415, "x": 796, "hp": true}, "tile4": {"y": 415, "x": 795, "hp": true}, "tile7": {"y": 416, "x": 792, "hp": true}, "tile6": {"y": 415, "x": 797, "hp": true}, "tile9": {"y": 416, "x": 794, "hp": true}, "tile8": {"y": 416, "x": 793, "hp": true}, "zoom": " 10", "tile11": {"y": 416, "x": 796, "hp": true}, "tile10": {"y": 416, "x": 795, "hp": true}, "tile12": {"y": 416, "x": 797, "hp": true}}, {"tile1": {"y": 416, "x": 792, "hp": true}, "index": "2", "tile3": {"y": 415, "x": 794, "hp": true}, "tile2": {"y": 415, "x": 793, "hp": true}, "tile5": {"y": 415, "x": 796, "hp": true}, "tile4": {"y": 415, "x": 795, "hp": true}, "tile7": {"y": 416, "x": 792, "hp": true}, "tile6": {"y": 415, "x": 797, "hp": true}, "tile9": {"y": 416, "x": 794, "hp": true}, "tile8": {"y": 416, "x": 793, "hp": true}, "zoom": " 10", "tile11": {"y": 416, "x": 796, "hp": true}, "tile10": {"y": 416, "x": 795, "hp": true}, "tile12": {"y": 416, "x": 797, "hp": true}} ];
loopRespData(data); 

完整的代码是

Book

这是小提琴

https://jsfiddle.net/Refatrafi/8hw64pgt/11/

答案 2 :(得分:1)

第一个循环遍历每个项目。每个项目看起来像:{"tile1": {"y": 212, "x": 392, "hp": true}, "index": "1" ...}所以没有长度属性可供阅读。

此外,第二个循环计算整数(1,2,3 ......)并尝试在第一个项目上获取属性。第一个项目上没有任何属性被称为&#34; 1&#34;,&#34; 2&#34;,&#34; 3&#34; ...

还有一些错字点缀在其中并没有帮助。您可以使用开发工具查看拼写错误创建的错误。

答案 3 :(得分:1)

您可以使用forEach()循环和Object.keys()

 var  data = [{"square1":{"y":212,"x":392,"hp":true},"index":"1","square3":{"y":415,"x":794,"hp":true},"square2":{"y":415,"x":793,"hp":true},"square5":{"y":415,"x":796,"hp":true},"square4":{"y":415,"x":795,"hp":true},"square7":{"y":416,"x":792,"hp":true},"square6":{"y":415,"x":797,"hp":true},"square9":{"y":416,"x":794,"hp":true},"square8":{"y":416,"x":793,"hp":true},"zoom":" 10","square11":{"y":416,"x":796,"hp":true},"square10":{"y":416,"x":795,"hp":true},"square12":{"y":416,"x":797,"hp":true}},{"square1":{"y":416,"x":792,"hp":true},"index":"2","square3":{"y":415,"x":794,"hp":true},"square2":{"y":415,"x":793,"hp":true},"square5":{"y":415,"x":796,"hp":true},"square4":{"y":415,"x":795,"hp":true},"square7":{"y":416,"x":792,"hp":true},"square6":{"y":415,"x":797,"hp":true},"square9":{"y":416,"x":794,"hp":true},"square8":{"y":416,"x":793,"hp":true},"zoom":" 10","square11":{"y":416,"x":796,"hp":true},"square10":{"y":416,"x":795,"hp":true},"square12":{"y":416,"x":797,"hp":true}}]

data.forEach(function(o) {
  Object.keys(o).forEach(function(e) {
    if(e.match(/^square/)) console.log("X:" + o[e].x + ' , ' + "Y: " + o[e].y + ' , hp: ' + o[e].hp);
  })
})