Last active
August 26, 2024 06:58
-
-
Save nblackburn/67a2578bc5bb4b49b1948699c1bc4f1e to your computer and use it in GitHub Desktop.
Break up an array into chunks of a given size.
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
module.exports = (data, size = 10) => { | |
let chunk = []; | |
return data.reduce((chunks, item, index) => { | |
chunk.push(item); | |
if (chunk.length >= size || index === data.length - 1) { | |
chunks.push(chunk); | |
chunk = []; | |
} | |
return chunks; | |
}, []); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment