Javascript 将多维数组转换为一维数组

Javascript 将多维数组转换为一维数组,javascript,arrays,multidimensional-array,Javascript,Arrays,Multidimensional Array,我有一个包含对象的数组 const nodes = [ { children: [1, 2, 3] }, { children: [1, 2, 3] } ]; 我想要一个新的数组[1,2,3,1,2,3] 我试过了 nodes.map(node => node.children); [].concat(nodes.map(node => node.children)); 但是它给了我[[1,2,3],[1,2,3]] 我试过了 nodes.map(node => node

我有一个包含对象的数组

const nodes = [ { children: [1, 2, 3] }, { children: [1, 2, 3] } ];
我想要一个新的数组
[1,2,3,1,2,3]

我试过了

nodes.map(node => node.children);
[].concat(nodes.map(node => node.children));
但是它给了我
[[1,2,3],[1,2,3]]

我试过了

nodes.map(node => node.children);
[].concat(nodes.map(node => node.children));
但它不起作用,因为它只是将
[]
[[1,2,3],[1,2,3]]
连接起来,而
[[1,2,3],[1,2,3]]
你可以用它来做这件事

const节点=[{children:[1,2,3]},{children:[1,2,3]}];
var result=nodes.reduce(函数(r,o){
r=r.concat(o.children);
返回r;
}, []);
console.log(结果)
您可以使用

const nodes=[{children:[1,2,3]},{children:[1,2,3]},
结果=nodes.reduce((r,node)=>r.concat(node.children),[]);
控制台日志(结果);
console.log([…新设置(结果)]);//对于唯一值

.as控制台包装{max height:100%!important;top:0;}
另一种方法是:

const节点=[{children:[1,2,3]},{children:[1,2,3]}]
最终=[]
nodes.forEach(x=>final=final.concat(x.children))

console.log(最终版)
谢谢!减少后,我将使用
.filter((item,pos,self)=>self.indexOf(item)==pos)
删除重复项。在
reduce
中执行此操作是否有意义,而不是在之后使用链接?您可以用于唯一值。它是否与
Array.from(new Set(result))
相同?