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]]

Source

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