如何使用单行解决方案将包含对象数组数组的对象减少为内部对象?
How to, with a one-lined solution, can I reduce an object containing arrays of arrays of objects to the inner objects?
我有以下对象结构:
regions: [
{
name: reg1,
subregions: [
{name: subreg11},
{name: subreg12}
]
},
{
name: reg2,
subregions: [
{name: subreg21},
{name: subreg22}
]
}
]
我想要的是提取子区域数组,如下所示:
[{name: subreg11}, {name: subreg12}, {name: subreg21}, {name: subreg22}]
...我想用一行 array.prototype 函数来做到这一点,例如 .filter、.map、.forEach 等
我已经设法用这样的三行来完成(包括 return 语句,我想在一行中包含它):
let subregions = []
regions.forEach(reg => reg.subregions.forEach(subregion => subregions.push(subregion)))
return locs
有人知道我该怎么做吗?
编辑 2020 年 7 月:我看到问题已被 @nina-sholz, but I don't believe this is true. The linked question asks "" 标记为重复,而我的问题是对解决问题的方法的描述. 我没有特别请求 flatMap 函数,将其标记为重复只是说“你应该凭直觉知道 Array.prototype.flatMap
是你要找的东西”。
flatMap
并提取 subregions
属性:
const regions = [
{
name: 'reg1',
subregions: [
{name: 'subreg11'},
{name: 'subreg12'}
]
},
{
name: 'reg2',
subregions: [
{name: 'subreg21'},
{name: 'subreg22'}
]
}
];
const output = regions.flatMap(({ subregions }) => subregions);
console.log(output);
您也可以使用 reduce
:
const regions = [
{
name: 'reg1',
subregions: [
{name: 'subreg11'},
{name: 'subreg12'}
]
},
{
name: 'reg2',
subregions: [
{name: 'subreg21'},
{name: 'subreg22'}
]
}
];
const output = regions.reduce((a, { subregions }) => a.concat(subregions), []);
console.log(output);
几乎任何 Javascript 代码都可以简化为一行,但不一定可读。
您可以使用 map
和 flat
const regions = [{name: 'reg1',subregions: [{name: 'subreg11'},{name: 'subreg12'}]},{name: 'reg2', subregions: [{name: 'subreg21'},{name: 'subreg22'}]}];
const output = regions.map(({ subregions }) => subregions).flat();
console.log(output);
我有以下对象结构:
regions: [
{
name: reg1,
subregions: [
{name: subreg11},
{name: subreg12}
]
},
{
name: reg2,
subregions: [
{name: subreg21},
{name: subreg22}
]
}
]
我想要的是提取子区域数组,如下所示:
[{name: subreg11}, {name: subreg12}, {name: subreg21}, {name: subreg22}]
...我想用一行 array.prototype 函数来做到这一点,例如 .filter、.map、.forEach 等
我已经设法用这样的三行来完成(包括 return 语句,我想在一行中包含它):
let subregions = []
regions.forEach(reg => reg.subregions.forEach(subregion => subregions.push(subregion)))
return locs
有人知道我该怎么做吗?
编辑 2020 年 7 月:我看到问题已被 @nina-sholz, but I don't believe this is true. The linked question asks "Array.prototype.flatMap
是你要找的东西”。
flatMap
并提取 subregions
属性:
const regions = [
{
name: 'reg1',
subregions: [
{name: 'subreg11'},
{name: 'subreg12'}
]
},
{
name: 'reg2',
subregions: [
{name: 'subreg21'},
{name: 'subreg22'}
]
}
];
const output = regions.flatMap(({ subregions }) => subregions);
console.log(output);
您也可以使用 reduce
:
const regions = [
{
name: 'reg1',
subregions: [
{name: 'subreg11'},
{name: 'subreg12'}
]
},
{
name: 'reg2',
subregions: [
{name: 'subreg21'},
{name: 'subreg22'}
]
}
];
const output = regions.reduce((a, { subregions }) => a.concat(subregions), []);
console.log(output);
几乎任何 Javascript 代码都可以简化为一行,但不一定可读。
您可以使用 map
和 flat
const regions = [{name: 'reg1',subregions: [{name: 'subreg11'},{name: 'subreg12'}]},{name: 'reg2', subregions: [{name: 'subreg21'},{name: 'subreg22'}]}];
const output = regions.map(({ subregions }) => subregions).flat();
console.log(output);