[英]JS: Flatten array of array while mapping through it?
If I want the colors of the following data, I know I can do: let colors = foo.foo.data.map(x => x.colors)
which will give me an array of arrays:如果我想要以下数据的 colors,我知道我可以这样做:
let colors = foo.foo.data.map(x => x.colors)
这会给我一个 ZA3CBC3F9D0CE2F2C1554E1B671 数组
`[['yellow', 'green'],['grey','pink'],['white', 'willow']] `[['黄色', '绿色'],['灰色','粉色'],['白色', '柳树']]
Is there a way to flatten that while mapping through it rather than doing a separate iteration to flatten after it's mapped?有没有办法在映射时将其展平,而不是在映射后进行单独的迭代以展平?
let foo = { foo: {
data: [
{
name: "asdd",
age: 55,
colors: ["red", "black"]
},
{
name: "Richard",
age: 12,
colors: ["yellow", "green"]
},
{
name: "Marcus",
age: 99,
colors: ["grey", "pink"]
},
{
name: "Ian",
age: 24,
colors: ["white", "willow"]
},
]
}}
Is there a way to flatten that while mapping through it rather than doing a separate iteration to flatten after it's mapped?
有没有办法在映射时将其展平,而不是在映射后进行单独的迭代以展平?
Yes, you can use .flatMap() which will flatten the returned results into one outer resulting array like so:是的,您可以使用.flatMap()将返回的结果展平为一个外部结果数组,如下所示:
let foo = { foo: { data: [{ name: "asdd", age: 55, colors: ["red", "black"] }, { name: "Richard", age: 12, colors: ["yellow", "green"] }, { name: "Marcus", age: 99, colors: ["grey", "pink"] }, { name: "Ian", age: 24, colors: ["white", "willow"] }, ] } }; let colors = foo.foo.data.flatMap(x => x.colors); console.log(colors);
You can use flatMap
.您可以使用
flatMap
。 It will create a flat array with the elements.它将创建一个包含元素的平面数组。
foo.foo.data.flatMap(x => x.colors)
let foo = { foo: { data: [{ name: "asdd", age: 55, colors: ["red", "black"] }, { name: "Richard", age: 12, colors: ["yellow", "green"] }, { name: "Marcus", age: 99, colors: ["grey", "pink"] }, { name: "Ian", age: 24, colors: ["white", "willow"] }, ] } } let colors = foo.foo.data.flatMap(x => x.colors) console.log(colors) <:-- begin snippet: js hide: false console: true babel: false -->
Or you can use reduce
and concat
too.或者你也可以使用
reduce
和concat
。
foo.foo.data.reduce((acc, x) => acc.concat(x.colors), []);
let foo = { foo: { data: [{ name: "asdd", age: 55, colors: ["red", "black"] }, { name: "Richard", age: 12, colors: ["yellow", "green"] }, { name: "Marcus", age: 99, colors: ["grey", "pink"] }, { name: "Ian", age: 24, colors: ["white", "willow"] }, ] } } let colors = foo.foo.data.reduce((acc, x) => acc.concat(x.colors), []); console.log(colors)
But this is inefficient and should be avoided for large arrays.但这是低效的,对于大型 arrays 应避免。
You can flat with reduce method, try this:你可以使用 reduce 方法,试试这个:
let foo = { foo: { data: [ { name: "asdd", age: 55, colors: ["red", "black"] }, { name: "Richard", age: 12, colors: ["yellow", "green"] }, { name: "Marcus", age: 99, colors: ["grey", "pink"] }, { name: "Ian", age: 24, colors: ["white", "willow"] }, ] }}; let colors = foo.foo.data.map(x => x.colors).reduce((a, b) => a.concat(b), []); console.log(colors)
Not as concise as flatMap()
but this also works:不如
flatMap()
简洁,但这也有效:
let colors = [];
foo.foo.data.forEach(elmt => colors = colors.concat(elmt.colors));
console.log(colors); // Array(8) [ "red", "black", "yellow", "green", "grey", "pink", "white", "willow" ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.