Skip to content

Instantly share code, notes, and snippets.

@nblackburn
Last active August 26, 2024 06:58
Show Gist options
  • Save nblackburn/67a2578bc5bb4b49b1948699c1bc4f1e to your computer and use it in GitHub Desktop.
Save nblackburn/67a2578bc5bb4b49b1948699c1bc4f1e to your computer and use it in GitHub Desktop.
Break up an array into chunks of a given size.
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