chunk
chunk walks the array once and slices it into groups of size elements. When the length does not divide evenly, the final group holds the remainder and is shorter than the rest. The input array is never modified.
Both arguments are validated before any work happens: a non-array input or a fractional size throws instead of returning a quietly wrong result. A size of 0 or less, and an empty input, both produce [] rather than a group of empty groups.
Parameters
Parameter | Type | Description |
|---|---|---|
|
| The array to split. Left untouched. |
|
| The length of each group. An integer; defaults to |
Returns
Array<T[]> — a new array of groups. Every group but the last has exactly size elements.
Throws
Exception | Condition |
|---|---|
|
|
|
|
Examples
Splitting into fixed-size groups
Writing a large range in batches
A single setValues call with tens of thousands of rows is the usual cause of an exceeded execution time. Splitting the data keeps each call small and each write independently recoverable.
Edge cases
See also
transpose— swaps the rows and columns of a matrix.
Source
This documentation was generated with AI (Claude) from the library's source code.