JavaScript Array flat()
Examples
Create a new array with one sub-array element concatenated:
const myArr = [1, 2, [3, [4, 5, 6], 7], 8];
const newArr = myArr.flat(1);
let text = JSON.stringify(newArr);
Try it Yourself »
Create a new array with two sub-array elements concatenated:
const myArr = [1, 2, [3, [4, 5, 6], 7], 8];
const newArr = myArr.flat(2);
let text = JSON.stringify(newArr);
Try it Yourself »
Note
Arrays displayed in HTML are automatically converted to strings.
To display the full structure (including brackets), use JSON.stringify().
Description
The flat() method concatenates sub-array elements.
Syntax
array.flat(depth)
Parameters
| Parameter | Description |
| depth | Optional. How deep a nested array should be flattened. Default is 1. |
Return Value
| Type | Description |
| An array | The flattened array. |
See Also:
More Examples
Array Tutorials:
Browser Support
JavaScript Array flat() is supported in all modern browsers since January 2020:
| Chrome 69 | Edge 79 | Firefox 62 | Safari 12 | Opera 56 |
| Sep 2018 | Jan 2020 | Sep 2018 | Sep 2018 | Sep 2018 |