javascript - 无法使用 jquery 循环列表

标签 javascript jquery

我定义了这样的 JSON:

 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}} ];

所以我想遍历其中的每个元素,并且想获取 data 的所有元素内每个项目中的 x 和 y。 我做了这样的功能:

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

但是控制台没有显示任何内容,因为第二个循环根本没有执行?有人可以帮我解决这个问题吗?我只需要获取 x、y 和 hp 的值。 JSFIDDLE

最佳答案

这里的 item 是一个对象而不是数组

    for(var key in item) {
      if (item.hasOwnProperty(key)) {
        var item2 = item[key];
        console.log("X:"+ item2.x + " Y"+ item2.y);
      }
    }

代替

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

这是 fiddle

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

关于javascript - 无法使用 jquery 循环列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192609/

相关文章:

javascript - 根据 var 从数组中选择一个值

javascript - Internet Explorer 中的 srcElement.readOnly 和 target.readOnly 问题

javascript - javascript 加载器是否取代了脚本组合的需要?

javascript - 禁用选定行的 href 链接

javascript - 比较 javascript/jquery 中的两个字符串

javascript - CSS或JS使垂直滚动条只在滚动时出现

javascript - 控制选择表单中的默认选择

javascript - Ajax 禁用表单字段

javascript - 在没有干预文本节点的情况下选择相邻的兄弟节点

javascript - 如何在 Bootstrap 4 中制作进度条动画?