Skip to content

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 { chunk } from "1o1-utils";
import { chunk } from "1o1-utils/chunk";
function chunk<T>({ array, size }: ChunkParams<T>): T[][]
NameTypeRequiredDescription
arrayT[]YesThe array to split
sizenumberYesMax items per chunk (positive integer)

T[][] — An array of arrays, each with at most size elements.

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"]]
  • Throws if array is not an array.
  • Throws if size is not a positive integer.
  • Returns [] for an empty array.

split array, batch, divide, paginate, group by size

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