chunk
Splits an array into smaller arrays (chunks) of a specified size. The last chunk may contain fewer elements if the array doesn’t divide evenly.
Import
Section titled “Import”import { chunk } from "1o1-utils";import { chunk } from "1o1-utils/chunk";Signature
Section titled “Signature”function chunk<T>({ array, size }: ChunkParams<T>): T[][]Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| array | T[] | Yes | The array to split |
| size | number | Yes | Max items per chunk (positive integer) |
Returns
Section titled “Returns”T[][] — An array of arrays, each with at most size elements.
Examples
Section titled “Examples”chunk({ array: [1, 2, 3, 4, 5], size: 2 });// => [[1, 2], [3, 4], [5]]
chunk({ array: ["a", "b", "c", "d"], size: 3 });// => [["a", "b", "c"], ["d"]]Edge Cases
Section titled “Edge Cases”- Throws if
arrayis not an array. - Throws if
sizeis not a positive integer. - Returns
[]for an empty array.
Also known as
Section titled “Also known as”split array, batch, divide, paginate, group by size
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use chunk to paginate a list of items client-side