I want to do this...
var all = [];
_.each(leftArr, (leftItem) => {
all.concat(leftItem.concat(getSomeOtherArray());
})
return all;
But I want to do it in a clean functional way. How can I do it without opening the foreach loop?
I want to do this...
var all = [];
_.each(leftArr, (leftItem) => {
all.concat(leftItem.concat(getSomeOtherArray());
})
return all;
But I want to do it in a clean functional way. How can I do it without opening the foreach loop?
Use Array#map to concat the leftItem
with the results of getSomeOtherArray()
, then flatten everything by spreading into another concat:
const leftArr = [[1], [2], [3]];
const getSomeOtherArray = () => ['a'];
const all = [].concat(...leftArr.map((leftItem) => leftItem.concat(getSomeOtherArray())));
console.log(all);