jQuery $.each() 带有嵌套数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15627934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$.each() with nested array
提问by Anthony Honciano
First of all, forgive me if I didn't identify the right type of array, however I can't seem to figure this out.
首先,如果我没有确定正确类型的数组,请原谅我,但是我似乎无法弄清楚这一点。
I'm trying to run this array in query:
我试图在查询中运行这个数组:
var myArray = {"artists":[{
"a1":"Adam Sandler",
"a2":"Adam Lambert",
"a3":"Avril Levine",
"a4":"Backstreet Boys",
"a5":"Blackstreet",
"a6":"Black Eye Peas",
"a7":"Cool and the Gang",
"a8":"Chicago",
"a9":"Charlie Manson"
}],
"songs":[{
"s1":"Grow Old With You",
"s2":"Whatdaya Want From Me",
"s3":"Yea yea",
"s4":"Quit Playing Games With My Heart",
"s5":"No Digity",
"s6":"Meet Me Half way",
"s7":"Doo wa ditty",
"s8":"Fight for your honor",
"s9":"Charlies Song"
}],
"genre":[{
"g1":"Pop",
"g2":"Pop",
"g3":"Alternative",
"g4":"R & B",
"g5":"R & B",
"g6":"Hip-Hop",
"g7":"Funk",
"g8":"Soft Rock",
"g9":"Rock"
}]};
When I click a button (say for title) I don't know how to have it automatically go through the array. This is what I have for my button:
当我点击一个按钮(比如标题)时,我不知道如何让它自动通过数组。这是我的按钮所拥有的:
$.each(myArray.songs, function(e,i){
console.log("e:"+e+" - i:"+i+" - "+myArray.songs[e].i);
});
This does work, however when it reaches to the console.log, this is what I get:
这确实有效,但是当它到达 console.log 时,这就是我得到的:
e:0 - i:[object Object] - undefined
e:0 - i:[object Object] - 未定义
I don't know how to get "i" to work, it always gives me [Object Object]. I replace I with the actual id in the array, it works.
我不知道如何让“i”工作,它总是给我 [Object Object]。我用数组中的实际 id 替换 I ,它有效。
Thank you.
谢谢你。
回答by PSL
If you want to use $.each
you can try this:-
如果你想使用$.each
你可以试试这个:-
$.each(myArray.songs, function (i, ob) {
$.each(ob, function (ind, obj) {
console.log("key:" + ind + " value:" + obj);
});
});
回答by Aiias
If you are trying to loop through all of the songs, myArray.songs
is an array with one object in it.
如果您试图循环播放所有歌曲,myArray.songs
则是一个包含一个对象的数组。
Try this:
尝试这个:
$.each(myArray.songs[0], function(e, i) {
console.log('e:' + e + ' - i:' + i);
});
And check out this jsFiddle http://jsfiddle.net/TsJP5/1/.
并查看这个 jsFiddle http://jsfiddle.net/TsJP5/1/。
回答by Matt Wielbut
Your structure is a bit strange - songs is an array with a single json element containing your key - value pairings. Nonetheless, to loop over these elements you should do:
您的结构有点奇怪 - 歌曲是一个数组,其中包含一个包含键值对的 json 元素。尽管如此,要遍历这些元素,您应该执行以下操作:
var songsElement = myArray.songs[0];
for (var key in songsElement) {
if (songsElement.hasOwnProperty(key)) {
console.log(key + " -> " + songsElement[key]);
}
}
回答by Christopher
Unless you need to iterate a collection of jQuery elements, I wouldn't use $.each() (this is personal preference). Rather, exploit the native forEach() method on Array:
除非您需要迭代一组 jQuery 元素,否则我不会使用 $.each() (这是个人偏好)。相反,利用 Array 上的原生 forEach() 方法:
function each(obj, onSuccess, recursive) {
if (obj && (typeof obj === 'object' || typeof obj === 'array')) {
Object.keys(obj).forEach(function(key) {
var val = obj[key];
if (onSuccess && val && key) {
var quit = onSuccess(val, key);
if (false === quit) {
return false;
}
}
if (true === recursive) {
each(val, onSuccess, true);
}
});
}
}
This handles both Array and Object instances as well as recursion. You take a slight performance hit by always using Object.keys()to get the member names, but I think its ultimately negligible even on very large data sets.
这处理 Array 和 Object 实例以及递归。你把一个轻微的性能命中通过始终使用Object.keys()来获取会员的名字,但我认为它最终可以忽略不计甚至在非常大的数据集。
回答by tjb1982
I'm not completely sure what your data means, but it looks like a bunch of song/artist/genre groupings that go together. E.g.,
我不完全确定您的数据意味着什么,但它看起来像是一堆组合在一起的歌曲/艺术家/流派分组。例如,
[{"song":"Losing My Religion",
"artist":"R.E.M.",
"genre":"alternative"},
{"song":"Bizarre Love Triangle",
"artist":"New Order",
"genre":"electronica"}]
I'm not sure why s1, a1 or g1 are useful, so I'm leaving them out. And then I honestly think using jquery for this isn't helpful. I would do this instead:
我不确定为什么 s1、a1 或 g1 有用,所以我将它们排除在外。然后老实说,我认为为此使用 jquery 没有帮助。我会这样做:
for (var i = 0; i < myArray.length; i++)
console.log(myArray[i].song + " by " + myArray[i].artist + " is of the " + myArray[i].genre + " ilk.");