Array.prototype.flat

Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.

Syntax

flat(depth?)

Parameters

Return value

A new flattened array.

Example

[1, [2, 3, [4, 5]]].flat()
[1, 2, 3, [4, 5]]
[1, [2, 3, [4, 5]]].flat(2)
[1, 2, 3, 4, 5]
[1, [2, 3, [4, 5]]].flat(Infinity)
[1, 2, 3, 4, 5]