Skip to main content

Function: forEach()

forEach<ElementType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => void): void

Executes a provided function once for each iterable element.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator to iterate over.
callbackFn(element: ElementType, index: number) => voidA function to execute for each element produced by the iterator. Its return value is discarded.

Returns

void

Example

import { forEach } from '@sapphire/iterator-utilities';

const iterable = [1, 2, 3, 4, 5];
forEach(iterable, (value) => console.log(value));
// Output: 1, 2, 3, 4, 5

Remarks

This function consumes the entire iterator.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/forEach.ts:24