Skip to content

chunk

The chunk() function splits an array into subarrays of a fixed size and returns the list of these blocks. The last block may be shorter if the size does not divide the array.

Interactive example

Syntax

Classic signature

typescript
function chunk<
  GenericInput extends readonly unknown[],
>(
  input: GenericInput,
  size: number,
): GenericInput[]

Curried signature

typescript
function chunk<
  GenericInput extends readonly unknown[],
>(
  size: number,
): (input: GenericInput) => GenericInput[]

Parameters

  • input: Array to split.
  • size: Size of each chunk.

Return value

A new array containing the created subarrays. The original array is not modified.

See also

  • slice - Extracts a portion of an array
  • flatMap - Maps then flattens the result

Released under the MIT license.