标签: javascript
我想这样做......
var all = []; _.each(leftArr, (leftItem) => { all.concat(leftItem.concat(getSomeOtherArray()); }) return all;
但我想以干净的功能方式做到这一点。如果不打开foreach循环,我怎么能这样做?
答案 0 :(得分:1)
使用Array#map将leftItem与getSomeOtherArray()的结果联系起来,然后将所有内容按spreading展平到另一个联合中:
leftItem
getSomeOtherArray()
const leftArr = [[1], [2], [3]]; const getSomeOtherArray = () => ['a']; const all = [].concat(...leftArr.map((leftItem) => leftItem.concat(getSomeOtherArray()))); console.log(all);