Function: map()
map<
ElementType,MappedType>(iterable:IterableResolvable<ElementType>,callbackFn: (element:ElementType,index:number) =>MappedType):IterableIterator<MappedType>
Defined in: projects/utilities/packages/iterator-utilities/src/lib/map.ts:20
Creates an iterable with the results of calling a provided function on each element.
Type Parameters
| Type Parameter |
|---|
ElementType |
MappedType |
Parameters
| Parameter | Type | Description |
|---|---|---|
iterable | IterableResolvable<ElementType> | An iterator to map over. |
callbackFn | (element: ElementType, index: number) => MappedType | A 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]