Iterator.prototype.chunks()

The chunks() method of Iterator instances returns a new iterator helper object that yields non-overlapping element sequences as arrays. Each time, the specified number of elements are retrieved from the underlying iterator and are yielded together as a chunk.

For yielding overlapping sequences (i.e., sliding window), see Iterator.prototype.windows().

Syntax

js
chunks(chunkSize)

Parameters

chunkSize

The number of elements in each chunk. Must be a positive integer less than 232 (the maximum array length).

Return value

A new iterator helper object. Each time the returned iterator helper's next() method is called, the current iterator is advanced by chunkSize elements, and those elements are yielded together as an array.

If the current iterator has some but fewer than chunkSize elements remaining, those elements are still yielded as an array (so the length is less than chunkSize), and the iterator helper will be immediately completed the next time next() is called.

If the current iterator has no elements remaining, the iterator helper is immediately completed without yielding an empty array.

Exceptions

TypeError

Thrown if chunkSize is not an integer.

RangeError

Thrown if chunkSize is less than 1 or greater than 232-1.

Examples

Printing elements in a grid

The following example creates an iterator that yields terms in the Fibonacci sequence. Then, they are printed in a grid layout by retrieving 5 elements each time.

js
function* fibonacci() {
  let current = 1;
  let next = 1;
  while (true) {
    yield current;
    [current, next] = [next, current + next];
  }
}

const rows = fibonacci().chunks(5);

for (const row of rows.take(5)) {
  console.log(row.join("\t"));
}

Output:

1       1       2       3       5
8       13      21      34      55
89      144     233     377     610
987     1597    2584    4181    6765
10946   17711   28657   46368   75025

Specifications

Specification
Iterator Chunking
# sec-iterator.prototype.chunks

Browser compatibility

See also