簡體   English   中英

無法使用jQuery循環瀏覽列表

[英]Can't loop through list with 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是一個對象,而不是數組。 您不能在這樣的對象上進行迭代。 (嚴格來說, 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": ... }
}

這是一個對象而不是數組,所以

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

這是小提琴

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

第一個循環遍歷每個項目。 每個項目看起來像: {"tile1": {"y": 212, "x": 392, "hp": true}, "index": "1" ...}因此,沒有要讀取的length屬性。

另外,第二個循環通過整數(1、2、3 ...)進行計數,並嘗試在第一個項目上獲得一個稱為那個的屬性。 在第一個項目上沒有名為“ 1”,“ 2”,“ 3”的屬性...

此外,還有一些錯別字,無濟於事。 您可以使用開發工具來查看由錯字造成的錯誤。

您可以使用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); }) }) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM