Skip to content

dependabit / utils/src / chunk

Function: chunk()

ts
function chunk<T>(arr, size): T[][];

Defined in: packages/utils/src/array.ts:76

Splits an array into consecutive chunks of the given size.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
arrT[]The array to chunk.
sizenumberMaximum number of elements per chunk.

Returns

T[][]

Array of chunks; the last chunk may be smaller than size.

Example

ts
chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]

Released under the MIT License.