我定义了这样的 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
关于javascript - 无法使用 jquery 循环列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192609/