🧩 Splits an array into groups of a specified size.

Syntax

TypeScript
import { chunk } from '@opentf/std';

chunk<T>(arr: T[] = [], size: number = 1): T[][]

Parameters

  • arr: The source array.

  • size: The length of each chunk. Defaults to 1.

Returns

A new array containing the chunks.

Examples

TypeScript
chunk([1, 2, 3]) //=> [[1], [2], [3]]

chunk([1, 2, 3], 1) //=> [[1], [2], [3]]

chunk([1, 2, 3], 2) //=> [[1, 2], [3]]

chunk([1, 2, 3], 3) //=> [[1, 2, 3]]

chunk([1, 2, 3], 4) //=> [[1, 2, 3]]
Last updated on
Edit this page