Skip to main content

Function: at()

at<ElementType>(iterable: IterableResolvable<ElementType>, index: number): undefined | ElementType

Defined in: projects/utilities/packages/iterator-utilities/src/lib/at.ts:27

Advances the iterable to the nth element and returns it. If the iterable is exhausted before reaching the nth element, it returns undefined.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator to return an element from.
indexnumberThe index of the element to retrieve.

Returns

undefined | ElementType

The element at the specified index, or undefined if the index is out of range.

Example

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

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

Remarks

This function consumes the input iterator up to the specified index.