Skip to content

Instantly share code, notes, and snippets.

@0ex-d
Created November 17, 2024 23:12
Show Gist options
  • Select an option

  • Save 0ex-d/36247d3484f1cf54818a469dd15c6b09 to your computer and use it in GitHub Desktop.

Select an option

Save 0ex-d/36247d3484f1cf54818a469dd15c6b09 to your computer and use it in GitHub Desktop.
Reusable Array chuncks in Javascript
export const chunkArray = (arr, size) => arr.reduce((chunks, el, idx) => {
if (idx % size === 0) {
chunks.push([el]);
} else {
chunks[chunks.length - 1].push(el);
}
return chunks;
}, []);
// Usage:
// chunkArray([1,2,3,4,5],2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment