Skip to content

Instantly share code, notes, and snippets.

@xat
Created November 5, 2014 17:17
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 xat/2cf34c8d3dd069a5ed42 to your computer and use it in GitHub Desktop.
Save xat/2cf34c8d3dd069a5ed42 to your computer and use it in GitHub Desktop.
group.js
var group = function(arr, cb, ctx) {
var buckets = [];
for (var i=0, len=arr.length; i<len; i++) {
if (cb.call(ctx, arr[i], i) || !buckets.length) {
buckets.push([arr[i]]);
} else {
buckets[buckets.length-1].push(arr[i]);
}
}
return buckets;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment