微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 带有嵌套数组的$.each()

首先,请原谅我,如果我没有找到正确的阵列类型,但我似乎无法弄清楚这一点.

我正在尝试在查询中运行此数组:

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"
        }]};

当我点击一个按钮(比如标题)时,我不知道怎么让它自动通过数组.这就是我的按钮:

$.each(myArray.songs,function(e,i){
                    console.log("e:"+e+" - i:"+i+" - "+myArray.songs[e].i);

                });

这确实有效,但是当它到达console.log时,这就是我得到的:

e:0 – i:[object Object] – undefined

我不知道如何让“我”工作,它总是给我[对象对象].我用数组中的实际id替换I,它可以工作.

谢谢.

解决方法

如果你想使用$.each,你可以试试这个: –

$.each(myArray.songs,function (i,ob) {
    $.each(ob,function (ind,obj) {
        console.log("key:" + ind + " value:" + obj);
    });
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐