flatMap
Import from @varavel/vdl-plugin-sdk/utils/arrays.
function flatMap<T, U, D>(
arr,
iteratee,
depth?
): FlatArray<U[], D>[];
Maps each element in the array using the iteratee function and flattens the result up to the specified depth.
Type Parameters
Parameters
| Parameter | Type | Description |
|---|---|---|
arr | readonly T[] | The array to flatten. |
iteratee | (item, index, array) => U | The function that produces the new array elements. It receives the element, its index, and the array. |
depth? | D | The depth level specifying how deep a nested array structure should be flattened. Defaults to 1. |
Returns
FlatArray<U[], D>[]
The new array with the mapped and flattened elements.
Example
const arr = [1, 2, 3];
flatMap(arr, (item: number) => [item, item]);
// [1, 1, 2, 2, 3, 3]
flatMap(arr, (item: number) => [[item, item]], 2);
// [1, 1, 2, 2, 3, 3]