Skip to content

Function: chunk()

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

Splits an array into groups with a fixed maximum size. 按固定最大长度将数组切分为多个分组。

Type Parameters

Type ParameterDescription
TThe type of array items / 数组元素类型

Parameters

ParameterTypeDescription
arrayreadonly T[]The source array / 源数组
sizenumberThe size of each chunk / 每组大小

Returns

T[][]

The chunked result / 分组后的结果

Example

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

Since

1.2.0

Released under the MIT License.