Function: chunk()
ts
function chunk<T>(arr: T[], len: number): T[][];
Splits an array into chunks of a specified length.
Type Parameters
Type Parameter | Default type | Description |
---|---|---|
T | any | The type of elements in the input array. |
Parameters
Parameter | Type | Description |
---|---|---|
arr | T [] | The array to split into chunks. |
len | number | The 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]]