Skip to main content

Function: map()

map<ElementType, MappedType>(iterable: IterableResolvable<ElementType>, callbackFn: (element: ElementType, index: number) => MappedType): IterableIterator<MappedType>

Creates an iterable with the results of calling a provided function on each element.

Type Parameters

Type Parameter
ElementType
MappedType

Parameters

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

Returns

IterableIterator<MappedType>

Example

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

const iterable = [1, 2, 3, 4, 5];
console.log([...map(iterable, (value) => value * 2)]);
// Output: [2, 4, 6, 8, 10]

Defined in

projects/utilities/packages/iterator-utilities/src/lib/map.ts:20