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
Name | Type | Optional | Description |
---|---|---|---|
list | T[] | undefined | No | Specifies the array, collection or enumerable object to iterate through. |
fn | ((...arguments: any[]) => void) | undefined | No | Specifies the reduce function to be invoked for each element. The return value of each reduce function will be the current reduced value. |
initial | R | Yes | Optional initial value for the reduce value. |
arguments | any[] | Yes | Optional 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`