DEV Community

Discussion on: Array Chunking

Collapse
 
schwarzwald profile image
schwarzwald

Or something like:

const chunk = (arr, n) => [...new Array(Math.ceil(arr.length / n))].map((x, i) => arr.slice(i * n, (i + 1) * n ))
Collapse
 
moopet profile image
Ben Sinclair

That's kind of hard to read (mostly because it's all on one loooong line).

Collapse
 
peralmq profile image
Pelle Almquist

Or even shorter 🤣

const chunk = ([...arr], n) => [...Array(Math.ceil(arr.length) / n)].map(_ => arr.splice(0, n))