Skip to content

Instantly share code, notes, and snippets.

@mike-burns
Created May 31, 2012 21:31
Show Gist options
  • Save mike-burns/2846438 to your computer and use it in GitHub Desktop.
Save mike-burns/2846438 to your computer and use it in GitHub Desktop.
groupBy in JS
Array.prototype.groupBy = function(f) {
var hash = {};
this.forEach(function(e) {
var result = f(e);
if (hash[result] == undefined)
hash[result] = [];
hash[result].push(e);
});
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment