Skip to main content

reduce

Functions

reduce()

reduce<T, U>(arrayOrPromise, callbackfn, initialValue): Promise<U>

Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

Type Parameters

Type Parameter
T
U

Parameters

ParameterTypeDescription
arrayOrPromiseT[] | Promise<T[]>The array or Promise of an array to iterate over.
callbackfn(previousValue, currentValue, currentIndex, array) => U | Promise<U>A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
initialValueUThe initial value of the accumulator.

Returns

Promise<U>

A Promise that resolves with the value that results from the reduction.

Example

const array = [1, 2, 3, 4];
const result = await reduce(array, (previousValue, currentValue) => previousValue + currentValue, 0);
console.log(result); // Output: 10

Defined in

src/reduce/index.ts:18