【发布时间】:2020-05-17 19:57:12
【问题描述】:
我有对象数组,我只取位置数组。我的目标是将这些位置数组合并到一个数组中,但是我没有这样做并得到空数组。我就是这样做的:
let results = [{
id: '1',
locations: ['aaaa', 'bbbbbb', 'cccccc']
},
{
id: '2',
locations: []
},
{
id: '3',
locations: ['ddd', 'aaadsad', 'sefd']
},
{
id: '4',
locations: ['ffff', 'eeee', 'sfdsfsd']
},
];
const locationIds = [].concat.apply([], ...results.filter(s => s.locations && s.locations.length > 0).map(({
locations
}) => ({
locations
})));
console.log(locationIds);
我在这里做错了什么?结果应该是
['aaaa', 'bbbbbb', 'cccccc', 'ddd', 'aaadsad', 'sefd', 'ffff', 'eeee', 'sfdsfsd'];
【问题讨论】:
标签: javascript arrays typescript