Skip to content

Instantly share code, notes, and snippets.

@KartikTalwar
Created October 10, 2016 22:58
Show Gist options
  • Save KartikTalwar/09841ff1c7a351d276c6cb32448feed4 to your computer and use it in GitHub Desktop.
Save KartikTalwar/09841ff1c7a351d276c6cb32448feed4 to your computer and use it in GitHub Desktop.
function groupBy(array, i) {
var groups = {};
array.forEach(function(o) {
var group = o[i];
groups[group] = groups[group] || [];
groups[group].push(o);
});
return groups
}
function groupByMulti(list, values) {
if (!values.length) {
return list;
}
var byFirst = groupBy(list, values[0]);
var rest = values.slice(1);
for (var prop in byFirst) {
byFirst[prop] = groupByMulti(byFirst[prop], rest);
}
return byFirst;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment