我有一个包含对象的数组
const nodes = [ { children: [1, 2, 3] }, { children: [1, 2, 3] } ];
我想要一个新的数组[ 1, 2, 3, 1, 2, 3 ]
。
我已经尝试过
nodes.map(node => node.children);
但是它给我返回了[ [ 1, 2, 3 ], [ 1, 2, 3 ] ]
。
我已经尝试过
[].concat(nodes.map(node => node.children));
但它不起作用,因为它只是将[]
与[[1,2,3],[1,2,3]]
连接起来,这只是[[1,2,3],[1,2,3]]
。
.filter((item, pos, self) => self.indexOf(item) == pos)
来删除重复项。这样做是否有意义,可以在reduce
内部完成,而不是之后进行链式操作? - mortensenSet
来获取唯一值。 - Nina ScholzArray.from(new Set(result))
是一样的吗? - mortensen