Skip to content

Instantly share code, notes, and snippets.

@dragonza
Last active February 19, 2018 10:49
Show Gist options
  • Select an option

  • Save dragonza/a45615fd33c5d1d35be198f95e6b916f to your computer and use it in GitHub Desktop.

Select an option

Save dragonza/a45615fd33c5d1d35be198f95e6b916f to your computer and use it in GitHub Desktop.
Chunk array
function chunk(array, size) {
const chunked_arr = [];
let copied = [...array]; // ES6 destructuring
const numOfChild = Math.ceil(copied.length / size); // Round up to the nearest integer
for (let i = 0; i < numOfChild; i++) {
chunked_arr.push(copied.splice(0, size));
}
return chunked_arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment