Created
January 31, 2017 20:16
-
-
Save lewisrodgers/efc3c060e3ffca67f912d26e5887f2fd to your computer and use it in GitHub Desktop.
Splits an array of objects into groups
This file contains hidden or 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
/** | |
* Expects data to be an array of objects. | |
* The key will be one of the keys in the objects. | |
*/ | |
function organizeBy(key, data) { | |
var results = {}; | |
data.forEach(function(obj) { | |
var value = obj[key]; | |
if (!results[value]) { | |
results[value] = []; | |
} | |
results[value].push(obj); | |
}) | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment