Skip to main content

Function: chunk()

Returns an iterable equivalent to iterable except its values are grouped into arrays that each contain size values.

The last array in the returned iterable will contain fewer than size values (but at least one) if the number of values in iterable is not divisible by size.

Throws

if size is not a positive integer.

Example

console.log(
pipe(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
chunk(3),
reduce(toArray()),
),
)
//=> [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]

console.log(
pipe(
[`S`, `L`, `O`, `T`, `H`],
chunk(2),
reduce(toArray()),
),
)
//=> [ [ 'S', 'L' ], [ 'O', 'T' ], [ 'H' ] ]

Since

v2.0.0

Call Signature

chunk<Size>(size): <Value>(iterable) => Iterable<Value[], any, any>

Type Parameters

Size extends number

Parameters

size

PositiveInteger<Size>

Returns

Function

Type Parameters

Value

Parameters

iterable

Iterable<Value, any, any>

Returns

Iterable<Value[], any, any>

Defined in

splices.d.ts:889

Call Signature

chunk<Size, Value>(size, iterable): Iterable<Value[], any, any>

Type Parameters

Size extends number

Value

Parameters

size

PositiveInteger<Size>

iterable

Iterable<Value, any, any>

Returns

Iterable<Value[], any, any>

Defined in

splices.d.ts:892