Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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