VDL Plugin SDK

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

Type ParameterDefault typeDescription
T-The type of elements within the array.
U-The type of elements within the returned array from the iteratee function.
D extends number1The depth to which the array should be flattened.

Parameters

ParameterTypeDescription
arrreadonly T[]The array to flatten.
iteratee(item, index, array) => UThe function that produces the new array elements. It receives the element, its index, and the array.
depth?DThe 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]