Files
30-seconds-of-code/test/flattenDepth/flattenDepth.js

4 lines
191 B
JavaScript

module.exports = flattenDepth = (arr, depth = 1) =>
depth != 1
? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), [])
: arr.reduce((a, v) => a.concat(v), []);