Skip to content

nth

nth<T>(iterable, index): T | undefined

Defined in: packages/core/src/iterable/nth.ts:21

Returns the element at index, or undefined when the iterable is shorter than that.

T cannot itself include undefined, so the absent case is never ambiguous.

Only counts forward. Array.prototype.at’s negative index would mean buffering the whole tail to know where the end is, so it is rejected rather than paid for silently — last covers the -1 case in O(1) for an array.

Time complexity: O(index). The iterator is closed as soon as the element is reached, without draining the rest.

T extends DefinedValue

Iterable<T>

The iterable to read from.

number

The zero-based position to read. Must be a non-negative integer.

T | undefined

The element at index, or undefined if there is none.

If index is not a non-negative integer.