D
Here's my solution with maxDepth: function flatten ( arr, maxDepth=Infinity, currDepth = 1 ) { let res = [] for (let i = 0 ; i < arr. length ; i + + ) { if ( ! Array.isArray(arr[i]) | | currDepth > maxDepth) { res. push (arr[i]) } else { res = res.concat(flatten(arr[i], maxDepth, + + currDepth)) } } return res }
