当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

chunk

优质
小牛编辑
133浏览
2023-12-01

Breaks this sequence into chunks (arrays) of a specified length.

Signature

Sequence.chunk = function(size) { /*...*/ }
Sequence.chunk = function chunk(size) {
  if (size < 1) {
throw new Error("You must specify a positive chunk size.");
  }

  return new ChunkedSequence(this, size);
}
NameType(s)Description
sizenumber

The size of each chunk.

returnsSequence

The resulting sequence of chunks.

Examples

Lazy([]).chunk(2)        // sequence: []
Lazy([1, 2, 3]).chunk(2) // sequence: [[1, 2], [3]]
Lazy([1, 2, 3]).chunk(1) // sequence: [[1], [2], [3]]
Lazy([1, 2, 3]).chunk(4) // sequence: [[1, 2, 3]]
Lazy([1, 2, 3]).chunk(0) // throws