Skip to content

Function: flatten()

ts
function flatten<T>(array, depth?): Flattened<T>[];

Flattens nested array items up to the specified depth. 按指定深度展开数组中的嵌套元素。

Type Parameters

Type ParameterDescription
TThe type of array items / 数组元素类型

Parameters

ParameterTypeDefault valueDescription
arrayreadonly T[]undefinedThe source array / 源数组
depthnumber1The 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

Released under the MIT License.