chunk - nodef/extra-lists GitHub Wiki

Break lists into chunks of given size.

function chunk(x, n, s)
// x: lists
// n: chunk size [1]
// s: chunk step [n]
const xlists = require('extra-lists');

var x = [
  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
  [1, 2, 3, 4, 5, 6, 7, 8]
];
xlists.chunk(x, 3);
// → [
// →   [ [ 'a', 'b', 'c' ], [ 1, 2, 3 ] ],
// →   [ [ 'd', 'e', 'f' ], [ 4, 5, 6 ] ],
// →   [ [ 'g', 'h' ], [ 7, 8 ] ]
// → ]

xlists.chunk(x, 2, 3);
// → [
// →   [ [ 'a', 'b' ], [ 1, 2 ] ],
// →   [ [ 'd', 'e' ], [ 4, 5 ] ],
// →   [ [ 'g', 'h' ], [ 7, 8 ] ]
// → ]

xlists.chunk(x, 4, 3);
// → [
// →   [ [ 'a', 'b', 'c', 'd' ], [ 1, 2, 3, 4 ] ],
// →   [ [ 'd', 'e', 'f', 'g' ], [ 4, 5, 6, 7 ] ],
// →   [ [ 'g', 'h' ], [ 7, 8 ] ]
// → ]

References