Skip to main content

Function: take()

take<ElementType>(iterable: IterableResolvable<ElementType>, count: number): IterableIterator<ElementType>

Creates an iterable with the first count elements.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterator to take values from.
countnumberThe maximum number of values to take from the iterator.

Returns

IterableIterator<ElementType>

An iterator that yields at most count values from the provided iterator.

Example

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

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

Defined in

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