frontend-challenges
frontend-challenges copied to clipboard
18 - flatten
index.js
export function flatten(arr, depth = 1) {
return arr.reduce((acc, value) => {
if (Array.isArray(value) && depth > 0) {
acc = acc.concat(flatten(value, depth - 1))
} else {
acc.push(value)
}
return acc;
}, [])
}