Skip to main content

Variable: chunk()

const chunk: {<Size>(size): <Value>(iterable) => Iterable<Value[]>; <Size, Value>(size, iterable): Iterable<Value[]>; }

Defined in: splices.d.ts:1021

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.

Call Signature​

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

Type Parameters​

Size​

Size extends number

Parameters​

size​

PositiveInteger<Size>

Returns​

<Value>(iterable): Iterable<Value[]>

Type Parameters​

Value​

Value

Parameters​

iterable​

Iterable<Value>

Returns​

Iterable<Value[]>

Call Signature​

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

Type Parameters​

Size​

Size extends number

Value​

Value

Parameters​

size​

PositiveInteger<Size>

iterable​

Iterable<Value>

Returns​

Iterable<Value[]>

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