gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:42:56 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com