chunk - nodef/extra-array GitHub Wiki

Break array into chunks of given size.

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

var x = [1, 2, 3, 4, 5, 6, 7, 8];
xarray.chunk(x, 3);
// → [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ]

xarray.chunk(x, 2, 3);
// → [ [ 1, 2 ], [ 4, 5 ], [ 7, 8 ] ]

xarray.chunk(x, 4, 3);
// → [ [ 1, 2, 3, 4 ], [ 4, 5, 6, 7 ], [ 7, 8 ] ]

References