Skip to content

Instantly share code, notes, and snippets.

@mminer
Created August 21, 2018 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mminer/c24f0586ae5956ee02e2fc955d840767 to your computer and use it in GitHub Desktop.
Save mminer/c24f0586ae5956ee02e2fc955d840767 to your computer and use it in GitHub Desktop.
Groups an array of objects by a common key.
function groupBy(values, key) {
return values.reduce((accumulator, value) => {
const id = value[key];
accumulator[id] = accumulator[id] || [];
accumulator[id].push(value);
return accumulator;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment