Skip to main content

chunk

Functions​

chunk()​

chunk<T>(arrayOrPromise, size): Promise<[T[]]>

Creates an array of arrays, where each array is a chunk of the provided array. The last chunk may contain less than the specified size.

Type Parameters​

Type ParameterDescription
TThe type of the elements in the array.

Parameters​

ParameterTypeDescription
arrayOrPromiseT[] | Promise<T[]>Array or Promise of an array to chunk
sizenumberThe length of each chunk

Returns​

Promise<[T[]]>

A Promise that resolves with a new array of arrays.

Example​

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

Defined in​

src/chunk/index.ts:19