Skip to content

Instantly share code, notes, and snippets.

@lewisrodgers
Created January 31, 2017 20:16
Show Gist options
  • Save lewisrodgers/efc3c060e3ffca67f912d26e5887f2fd to your computer and use it in GitHub Desktop.
Save lewisrodgers/efc3c060e3ffca67f912d26e5887f2fd to your computer and use it in GitHub Desktop.
Splits an array of objects into groups
/**
* 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