Standard built-ins
Array.prototype.flat()
Flattens actual nested arrays up to depth, default 1. Skips ordinary holes at levels traversed. Nonarray values remain as values, and the operation is not a recursive clone of arbitrary objects.
Formal Syntax & Structure
javascript syntaxjavascript
array.flat(depth?)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| depth | number | Optional nonnegative nesting depth after conversion and clamping; Infinity flattens finite nesting fully. |
Return Value
Array
Flattened result.
Working Example
Example Snippetjavascript
console.log(JSON.stringify([1,[2,[3]]].flat(2)));