Created
November 17, 2024 23:12
-
-
Save 0ex-d/36247d3484f1cf54818a469dd15c6b09 to your computer and use it in GitHub Desktop.
Reusable Array chuncks in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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