const arr = [[1,2,3,4,[6,7,8]],[5,6,7]]; console.log(_.flatMap(arr, duplicate)); console.log(flatMap(arr, duplicate)); function duplicate(n) { return [n, n]; } function flatMap(collection, interatee) { if (interatee instanceof Function) { collection = collection.map(item => interatee(item)); } let newArr = []; for (let item of collection) { if (item instanceof Array) { newArr = newArr.concat(flatMap(item)); } else { newArr.push(item); } } return newArr; }