xesa xesa - 3 months ago 15
Javascript Question

Can't loop through list with jquery

I defined a JSON like this:

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


So I want to go through each element inside this and I want to get x and y inside each item inside all elements of
data
.
I did a function like this:

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


But the console doesn't show anything because the second loop is not executed at all? Can someone help me on this, please? I just need to get the value of x, y and hp.
JSFIDDLE

Answer

here item is an object not an array so do

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

in place of

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

the full code is

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

Here is the fiddle

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