Skip to content

Function: chunk()

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

Splits an array into chunks of a specified length.

Type Parameters

Type ParameterDefault typeDescription
TanyThe type of elements in the input array.

Parameters

ParameterTypeDescription
arrT[]The array to split into chunks.
lennumberThe maximum length of each chunk.

Returns

T[][]

An array of arrays, where each sub-array is a chunk of the original array.

Example

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