Skip to main content

Function: starMap()

starMap<ElementType, MappedType>(iterable: IterableResolvable<ElementType>, callbackFn: (...args: StarMapParameters<ElementType>) => MappedType): IterableIterator<MappedType>

Creates an iterable with the results of calling a provided function on each element of the input iterables as the function's parameters.

Type Parameters

Type Parameter
ElementType extends IterableResolvable<any>
MappedType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterable to map over.
callbackFn(...args: StarMapParameters<ElementType>) => MappedTypeThe callback function to apply to each element.

Returns

IterableIterator<MappedType>

An iterable iterator that yields the mapped elements.

Example

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

const iterable = [[1, 2], [3, 4], [5, 6]];
console.log([...starMap(iterable, (a, b) => a + b)]);
// Output: [3, 7, 11]

Remarks

While very similar to map, starMap takes an iterable of iterables (which can be an array of tuples) and calls the function with each inner iterable's values as the function's parameters. map calls the function with the value and the index by comparison.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/starMap.ts:27