我有这个数组:[ 0 => { id: 1, name: 'Test 1', clients: [ 0 => { id: 100, name: 'Client 1' } ] }, 1 => { id: 2, name: 'Test 2', clients: [ 0 => { id: 101, name: 'Client 2' } ] }]在我的案例中,获取所有子数组数据的最佳方法和最短方法是什么clients:[ 0 => { id: 100, name: 'Client 1' }, 1 => { id: 101, client: 'Client 2' }]提前致谢,对不起我的英语。
1 回答

喵喔喔
TA贡献1735条经验 获得超5个赞
您可以使用 reduce 并在回调中使用concat创建新数组
let data = [{
id: 1,
name: 'Test 1',
clients: [{
id: 100,
name: 'Client 1'
}]
},
{
id: 2,
name: 'Test 2',
clients: [{
id: 101,
name: 'Client 2'
}]
}
]
let newData = data.reduce((acc, curr) => {
return acc.concat(curr.clients)
}, []);
console.log(newData)
添加回答
举报
0/150
提交
取消