Skip to main

reduce

reduce

Signature - source.ts#L1934

function reduce<T, R, I>(
    transform: (
        previousAccumulatedResult: R | I,
        currentValue: T,
        currentIndex: number,
    ) => R,
    initialValue: I,
): Operator<T, R>

Calls the specified transform function for all the values pushed by the given source. The return value of the transform function is the accumulated result, and is provided as an argument in the next call to the transform function. The accumulated result will be emitted as a Push event once the given source ends.

Parameters

ParameterTypeDescription
transform
(
    previousAccumulatedResult: R | I,
    currentValue: T,
    currentIndex: number,
) => R

A function that transforms the previousAccumulatedResult (last value returned by this function), the currentValue of the emitted Push event and the currentIndex, and returns an accumulated result.

initialValue
I

This is used as the initial value to start the accumulation. The first call to the transform function provides this as the previousAccumulatedResult.