Open this example in the interactive library
Source code
const chunk = (xs, n) =>
Array.from({ length: Math.ceil(xs.length / n) }, (_, i) =>
xs.slice(i * n, (i + 1) * n),
);
console.log(chunk([1, 2, 3, 4, 5], 2));
Sample output
[ [ 1, 2 ], [ 3, 4 ], [ 5 ] ]
