Skip to main content

Function: enumerate()

enumerate<ElementType>(iterable: IterableResolvable<ElementType>): IterableIterator<[number, ElementType]>

Creates a new iterable that yields the index and value of each element.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator to enumerate.

Returns

IterableIterator<[number, ElementType]>

An iterator that yields the index and value of each element in the source iterator.

Example

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

const iterable = ['a', 'b', 'c'];
for (const [index, value] of enumerate(iterable)) {
console.log(`Index: ${index}, Value: ${value}`);
// Output: Index: 0, Value: a
// Output: Index: 1, Value: b
// Output: Index: 2, Value: c
}

Defined in

projects/utilities/packages/iterator-utilities/src/lib/enumerate.ts:23