无法通过列表循环使用jQuery
我这样定义一个JSON:无法通过列表循环使用jQuery
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
。 我做了这样的功能:
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
这里的项目是一个对象不是一个数组这么做
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);
这里是小提琴
非常感谢您的回答。我有另一个问题?正如你可以看到有一些像索引和缩放的值,我怎么能忽略这些,因为当我执行此代码时,我得到未定义? – xesa
我编辑过。你可以再次检查object.hasOwnProperty('x')和object.hasOwnProperty('y')。 –
第一个循环遍历每个项目。每个项目看起来像:{"tile1": {"y": 212, "x": 392, "hp": true}, "index": "1" ...}
所以没有长度属性阅读。
此外,第二个循环通过整数(1,2,3 ...)进行计数,并试图在第一个项目上获得一个属性。在第一个项目上没有属性被称为“1”,“2”,“3”......
此外,还有一些点缀在其中的错字没有帮助。您可以使用开发工具查看错别字创建的错误。
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": ... }
}
您可以使用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);
})
})
为什么要使用枚举性能*(tile1,tile2, ...)*而不是一个数组? – Thomas