在里面使用.each .each

时间:2014-10-10 19:28:50

标签: jquery

我有JSON字符串,它嵌套了这样的对象数组

[{
    "date": 1412966290618,
        "memberInfo": {
        "firstName": "Antonio",
            "lastName": "Smith"
    },
        "service": [{
        "id": null,
            "desc": "Advice"
    }, {
        "id": null,
            "desc": "3rd Party Contact"
    }],
        "commentList": [{
        "createDate": "10/10/2014",
            "cmntType": "Notes",
            "cmntText": "A positive attitude can destroy every negative obstacle."
    }, {
        "createDate": "10/10/2014",
            "cmntType": "Notes",
            "cmntText": "I am blessed my grateful heart is the fountain of gladness"
    }]
}]

我试图使用$ .each()来循环所有元素。我能够读取“date”和“memberInfo”的值,但无法准备/访问其他两个。我想尝试

$.each(data, function (index, value) {
    var date = value.date;
    var memberName = value.memberInfo.firstName + ' ' + value.memberInfo.lastName;
    var serviceList = '';
    $.each(data.service, function (srvindex, service) {
      serviceList += '[' + service.id + ', ' + service.desc + ']';
    });
    var cmntList = '';
    $.each(data.commentList, function(cmntindex, cmnt) {
      cmntList += '[' + cmnt.createDate+ ', ' + cmnt.cmntText + ']';
    });
});

1 个答案:

答案 0 :(得分:6)

您应该使用value.servicevalue.commentList代替data.servicedata.commentList