Skip to main content

each

Iterates through an array, collection or enumerable object and invokes the supplied function for each element.

Signature

each<T>(
list: T[] | undefined,
fn: (item: T, ...arguments: any[]) => T | void,
options?: {
replace?: boolean;
keys?: boolean;
arguments?: any[];
}
): T[] | undefined

Parameters

NameTypeOptionalDescription
listT[] | undefinedNoSpecifies the array, collection or enumerable object to iterate through.
fn(item: T, ...arguments: any[]) => T | voidNoSpecifies the function to be invoked for each element. The element value will be exposed to the function as the first argument of the argument list. Additional arguments can be specified and will be pushed to the function.
optionsobjectYesSpecifies options for the iteration. Supports the following options:
- replace: Specifies if each value in the array should be overwritten with the return value of its executed function;
- keys: Specifies if the item key should be passed as first argument in the argument list of the function;
- arguments: Optional additional arguments which will be passed to the callee.

Return value

Returns a reference to the list.

Example

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

let nResult = 0;

each([1, 2, 3], (n) => nResult += n);
// The value of `nResult` is `6`