flat()
ES2019+Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Syntax
array.flat(depth)Parameters
depth number optionalThe depth level specifying how deep a nested array structure should be flattened (default: 1)
Return Value
Array
A new array with the sub-array elements concatenated
Examples
JavaScript
const nested = [1, [2, [3, [4]]]];
console.log(nested.flat());
console.log(nested.flat(2)); Output:
// [1, 2, [3, [4]]]
[1, 2, 3, [4]]