flatten
Import from @varavel/vdl-plugin-sdk/utils/arrays.
function flatten<T, D>(arr, depth?): FlatArray<T[], D>[];
Flattens an array up to the specified depth.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
T | - | The type of elements within the array. |
D extends number | 1 | The depth to which the array should be flattened. |
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | readonly T[] | The array to flatten. |
depth? | D | The depth level specifying how deep a nested array structure should be flattened. Defaults to 1. |
Returns
FlatArray<T[], D>[]
A new array that has been flattened.
Example
const arr = flatten([1, [2, 3], [4, [5, 6]]], 1);
// Returns: [1, 2, 3, 4, [5, 6]]
const arr = flatten([1, [2, 3], [4, [5, 6]]], 2);
// Returns: [1, 2, 3, 4, 5, 6]