chunk
Chunks an array into smaller arrays of specified size.
null and undefined are treated as empty arrays and return [].
Available since v1.9.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | null | undefined | The array to chunk |
size | number | The size of each chunk |
Returns
Section titled “Returns”T[][] — Array of chunks
Examples
Section titled “Examples”Split an array into pairs
Section titled “Split an array into pairs”Chunks an array of 5 elements into groups of 2, with the last chunk containing the remainder.
Handle exact divisions
Section titled “Handle exact divisions”When the array length is evenly divisible by the chunk size, all chunks are equal.
Return empty array for invalid size
Section titled “Return empty array for invalid size”A size of 0 or negative returns an empty array.
