Skip to main content

Function: last()

last<ElementType>(iterable: IterableResolvable<ElementType>): ElementType | undefined

Consumes the iterable until it's exhausted, returning the last element.

Type parameters

Type parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator to return the last value of.

Returns

ElementType | undefined

The value at the last position in the source iterator, or undefined if the iterator is empty.

Example

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

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

Remarks

This function consumes the entire iterator to find the last value.

Source

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