Skip to content

Instantly share code, notes, and snippets.

@srph
Created January 12, 2017 15:27
Show Gist options
  • Select an option

  • Save srph/f534c332f80073dd1899f723c1f0f02c to your computer and use it in GitHub Desktop.

Select an option

Save srph/f534c332f80073dd1899f723c1f0f02c to your computer and use it in GitHub Desktop.
JS: Group By
module.exports = function groupBy(arr, fn) {
var group = {};
arr.forEach(function(v, k) {
var prop = group[fn(v, k)];
prop == null ? prop = [v] : prop.push(v);
});
return group;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment