如何使用Functional JS连接数组?

时间:2017-11-09 20:17:26

标签: javascript

我想这样做......

var all = [];
_.each(leftArr, (leftItem) => { 
   all.concat(leftItem.concat(getSomeOtherArray());
})
return all;

但我想以干净的功能方式做到这一点。如果不打开foreach循环,我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

使用Array#mapleftItemgetSomeOtherArray()的结果联系起来,然后将所有内容按spreading展平到另一个联合中:



const leftArr = [[1], [2], [3]];

const getSomeOtherArray = () => ['a'];

const all = [].concat(...leftArr.map((leftItem) => leftItem.concat(getSomeOtherArray())));

console.log(all);