Skip to content

Instantly share code, notes, and snippets.

@dragonza
Last active July 10, 2020 17:15
Show Gist options
  • Save dragonza/17911fd008b8e3e0719a63f75f4348e0 to your computer and use it in GitHub Desktop.
Save dragonza/17911fd008b8e3e0719a63f75f4348e0 to your computer and use it in GitHub Desktop.
Chunk array
function chunk(array, size) {
const chunked_arr = [];
for (let i = 0; i < array.length; i++) {
const last = chunked_arr[chunked_arr.length - 1];
if (!last || last.length === size) {
chunked_arr.push([array[i]]);
} else {
last.push(array[i]);
}
}
return chunked_arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment