jquery每个循环遍历具有多个对象的数组

时间:2015-02-05 01:40:52

标签: jquery arrays each nested-loops javascript-objects

我有一个包含多个对象的javascript数组,如下图所示:

var myArray = [{

year:'2015',
model:'BMW',
used:false,
extras: [{buckets:'no',sportWheels:'yes'},{buckets:'no',sportWheels:'yes'}],
color:'blue'

.....

}];

我想要做的是循环这个数组并将结果传递给一个列表,如下所示:

$.each(myArray, function(i, e){
var year = myArray[i].year;
var model = myArray[i].model;
var used = myArray[i].used;
var color = myArray[i].color;
var extras = myArray[i].extras; 

$('.list').append(year+model+used+color);
});

一切顺利,直到我想收集临时演员的结果。我不知道该做什么可以有人提出建议。我想到了第二个循环,但不起作用!!!

1 个答案:

答案 0 :(得分:2)

为myArray [i] .extras

执行每个循环
    $.each(myArray, function(i, e){
        var year = myArray[i].year;
        var model = myArray[i].model;
        var used = myArray[i].used;
        var color = myArray[i].color;
        var extras = myArray[i].extras; 
        var str=''; 
        $.each(myArray[i].extras, function (index, data) {
            // alert(data.buckets);
            str+='<div>' + year + model + used + color + data.buckets + data.sportWheels +'</div>';
        })
        $('.list').append(str);
        //$('.list').append(year+model+used+color);
    });