[英]Jquery: Iterating over nested JSON with unique key names
I have a large list of objects inside JSON like this: 我在JSON中有一大堆对象,如下所示:
var data = {
4eae1aa12efa83745d00000b: {
location: "office",
latLong: [
40.7069546, -74.0094471
],
},
4eae1aa12efa83745d000000: {
location: "home",
latLong: [
42.3584308, -71.0597732
]
}
};
Where the 4eae1aa12efa83745d00000b
style key is random. 4eae1aa12efa83745d00000b
样式密钥是随机的。 How do I iterate through the JSON to print the location and latLong array of each nested JSON object? 如何遍历JSON以打印每个嵌套JSON对象的位置和latLong数组?
I tried: 我试过了:
$.each(data, function() {
$.each(this, function() {
console.log(this.location);
});
});
but that doesn't return anything 但这不会返回任何东西
You should look up the $.map function to translate the items in your object/array - Go for something like this: 你应该查找$ .map函数来翻译你的对象/数组中的项目 - 转到这样的事情:
$.map(data, function(val, i){
console.log(val.location);
console.log(val.latLong[1]);
console.log(val.latLong[2]);
})
I believe that's what you're after anyway. 无论如何,我相信这就是你所追求的。
Your only problem is that you don't need the inner loop. 你唯一的问题是你不需要内循环。
$.each(data, function(id, value) {
console.log(value.location);
});
You are trying to loop over the properties of an object. 您正在尝试遍历对象的属性。 To do this:
去做这个:
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
// do something with obj[prop].latLng
}
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.