Skip to main content

Function: chunk()

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

Chunks the iterable into arrays of at most size elements.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterator whose elements to chunk.
sizenumberThe maximum size of each chunk.

Returns

IterableIterator<ElementType[]>

Example

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

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

Defined in

projects/utilities/packages/iterator-utilities/src/lib/chunk.ts:21