Skip to content

Instantly share code, notes, and snippets.

@erictleung
Created November 30, 2015 08:54
Show Gist options
  • Save erictleung/122baea529eb7cfcb003 to your computer and use it in GitHub Desktop.
Save erictleung/122baea529eb7cfcb003 to your computer and use it in GitHub Desktop.
Breakup an array into an array of arrays of a given size
function chunk(arr, size) {
var final = [];
for (i = 0; i < Math.ceil(arr.length/size); i++) {
final.push(arr.slice(i*size, (i+1)*size));
}
return final;
}
chunk(["a", "b", "c", "d", "e"], 2); // returns [["a", "b"], ["c", "d"], ["e"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment