$ .each()与嵌套数组 [英] $.each() with nested array
问题描述
首先,如果我没有确定正确的数组类型,请原谅我,但是我似乎无法弄清楚.
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);
});
这确实有效,但是当到达console.log时,这就是我得到的:
This does work, however when it reaches to the console.log, this is what I get:
e:0-i:[对象对象]-未定义
e:0 - i:[object Object] - undefined
我不知道如何使"i"正常工作,它总是给我[Object Object].我将I替换为数组中的实际ID,它可以正常工作.
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.
谢谢.
推荐答案
如果要使用$.each
,可以尝试以下操作:-
If you want to use $.each
you can try this:-
$.each(myArray.songs, function (i, ob) {
$.each(ob, function (ind, obj) {
console.log("key:" + ind + " value:" + obj);
});
});
这篇关于$ .each()与嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!