Skip to main content

reduce

Reduce boils down an array, collection or enumerable object into a single value of type R.

Signature

reduce<T, R>(
list: TList<T> | undefined,
fn: (value: R, item: T, ...arguments: any[]) => R,
initial?: R,
...arguments: any[]
): R | undefined

Parameters

NameTypeOptionalDescription
listT[] | undefinedNoSpecifies the array, collection or enumerable object to iterate through.
fn((...arguments: any[]) => void) | undefinedNoSpecifies the reduce function to be invoked for each element. The return value of each reduce function will be the current reduced value.
initialRYesOptional initial value for the reduce value.
argumentsany[]YesOptional arguments which should be passed to the callee.

Return value

Returns the reduced value.

Example

import { reduce } from "@tripetto/runner";

reduce<number, number>([1, 2, 3], (nValue: number, nItem: number) => nValue + nItem, 2); // Returns `8`