javascript - 在 javascript 中返回数组和子数组

标签 javascript jquery arrays loops

未显示带有子数组的数组循环。友善的建议。我能够显示第一个数组集,但无法显示每组的子数组。

var json = [{
"Friends":[
    {
        'image'     : '_assets/images/users/01.jpg',
        'unread'    : '22',
        'name'      : 'Salman Razak',
        'message'   : 'way to be happy...',
        'lastchat'  : '16th Feb 2015 | 9:30 pm'
    },
    {
        'image'     : '_assets/images/users/02.jpg',
        'unread'    : '22',
        'name'      : 'Shahid Saeed',
        'message'   : 'way to be happy...',
        'lastchat'  : '16th Feb 2005 | 9:30 pm'
    }
],
"Colleagues":[
    {
        'image'     : '_assets/images/users/02.jpg',
        'unread'    : '22',
        'name'      : 'Hyder Memon',
        'message'   : 'way to be happy...',
        'lastchat'  : '16th Feb 2015 | 9:30 pm'
    }
]
}];

$.each(json, function () {
   $.each(this, function (name, value) {
      console.log(name + '=' + value);
      $('ul').append('<li>'+ name + ', ' + json[name].join() +'</li>');
   });
});

最佳答案

这是一个适用于您拥有的数据结构的循环 ( working jsbin )

json.forEach(function(item){
    for(var group in item){
        if(item.hasOwnProperty(group)){
            var groupItems = item[group];
            groupItems.forEach(function(person){
                console.log(group, person.name);
            });
        }
    }
});

关于javascript - 在 javascript 中返回数组和子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330677/

相关文章:

javascript - For...of 循环调用字符串中的变量名称

javascript - Mongodb/ Mongoose : How to properly implement findOneAndUpdate on an express route

javascript - 如何从表 tr td 中获取值

java - 比较两个数组。处理这种情况的明智方法是什么?

javascript - 黑暗模式在重新加载时闪烁白色背景一毫秒

php - 在javascript中更改选定的组合框值

javascript - <select> 的 jQuery .clone() : change the selected option

javascript - 在 jQuery 中显示/隐藏子列表项

arrays - n x n 字符数组中的字符组合 :

javascript - AngularJS ng-repeat 在填充表时创建三个空行并且 ng-repeat 不重复