GuobaGuoba Utils

chunk

@guoba-ai/utils / array / chunk

Function: chunk()

function chunk<T>(array, size): T[][];

Defined in: array.ts:109

Split an array into chunks of the given size.

Parameters

array

T[]

The array to split

size

number

The size of each chunk

Returns

T[][]

An array of chunks

Example

chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]]

chunk([1, 2, 3, 4], 2) // [[1, 2], [3, 4]]

chunk([1, 2], 5) // [[1, 2]]

Warning

size must be a positive integer. 0 or negative values will not produce a valid result.

On this page