Created
October 10, 2016 22:58
-
-
Save KartikTalwar/09841ff1c7a351d276c6cb32448feed4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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