Skip to main content

flatMap

Functions

flatMap()

flatMap<T, U>(arrayOrPromise, callbackfn): Promise<U[]>

Calls a defined callback function on each element of an array, and then flattens the result into a new array.

Type Parameters

Type Parameter
T
U

Parameters

ParameterTypeDescription
arrayOrPromiseT[] | Promise<T[]>The array or Promise of an array to iterate over.
callbackfn(value, index, array) => U[] | Promise<U[]>A function that accepts up to three arguments. The flatMap method calls the callbackfn function one time for each element in the array.

Returns

Promise<U[]>

A Promise that resolves with a new array with the results of calling a provided function on every element in the calling array and flattening the result into a new array.

Example

const array = [1, 2, 3, 4];
const result = await flatMap(array, (value) => [value, value * 2]);
console.log(result); // Output: [1, 2, 2, 4, 3, 6, 4, 8]

Defined in

src/flatMap/index.ts:17