Split arrays into batches
Break arrays into predictable batches for processing or pagination.
Original Pearto instructional example. Test it in your own environment and adapt it to your requirements.
function chunk(items, size) {
if (!Number.isInteger(size) || size < 1) throw new RangeError('size');
return Array.from({length: Math.ceil(items.length / size)}, (_, i) => items.slice(i * size, (i + 1) * size));
}
console.log(chunk([1,2,3,4,5], 2));