Function: flatten()
ts
function flatten<T>(array, depth?): Flattened<T>[];Flattens nested array items up to the specified depth. 按指定深度展开数组中的嵌套元素。
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of array items / 数组元素类型 |
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
array | readonly T[] | undefined | The source array / 源数组 |
depth | number | 1 | The number of levels to flatten / 要展开的层级 |
Returns
Flattened<T>[]
A new flattened array / 展开后的新数组
Example
ts
flatten([1, [2, [3]], 4], 1)
// => [1, 2, [3], 4]Since
1.2.0