Skip to content

Instantly share code, notes, and snippets.

@foo9
Created May 2, 2013 03:22
Show Gist options
  • Select an option

  • Save foo9/5499960 to your computer and use it in GitHub Desktop.

Select an option

Save foo9/5499960 to your computer and use it in GitHub Desktop.
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
var size = 2;
var result = chunk(array, size);
// console.log(result); // [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g']]
function chunk(array, size) {
var len = array.length;
var index = 0;
var result = [];
if (len < 1 || len < size) {
return array;
}
do {
result.push(array.slice(index, index + size));
index = index + size;
} while (index < len);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment