const arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12]]]
function flat(arr, depth = 1) {
const resultArr = [];
arr.forEach(item => {
if (Array.isArray(item) && depth > 0) {
resultArr.push(...flat(item, depth - 1))
} else {
resultArr.push(item)
}
})
return resultArr
}
console.log(flat(arr, 2))
function flatReduce(arr, depth = 1) {
return arr.reduce((result, item) => {
if (Array.isArray(item) && depth >= 0) {
return result.concat(flatReduce(item, depth - 1))
} else {
return result.concat(item)
}
}, [])
}
console.log(flatReduce(arr, Infinity))