Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Use Array.flat if possible or polyfill globally:
Array.prototype.flat = function(depth) { return flatten(this, depth)} Copy
Array.prototype.flat = function(depth) { return flatten(this, depth)}
Optional
The maximum recursion depth
flatten([1, 2, [3, 4]])// [1, 2, 3, 4]flatten([1, 2, [3, 4, [5, 6]]])// [1, 2, 3, 4, [5, 6]]flatten([1, 2, [3, 4, [5, 6]]], 2)// [1, 2, 3, 4, 5, 6] Copy
flatten([1, 2, [3, 4]])// [1, 2, 3, 4]flatten([1, 2, [3, 4, [5, 6]]])// [1, 2, 3, 4, [5, 6]]flatten([1, 2, [3, 4, [5, 6]]], 2)// [1, 2, 3, 4, 5, 6]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Use Array.flat if possible or polyfill globally: