Skip to content

Instantly share code, notes, and snippets.

@nblackburn
Created March 18, 2021 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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, limit = 10) => {
let chunk = -1;
let chunks = [];
for (let index = 0; index < data.length; index++) {
if (index % limit === 0) {
chunk += 1;
chunks.push([]);
}
if (data[index]) {
chunks[chunk].push(data[index]);
}
}
return chunks;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment