Skip to content

Instantly share code, notes, and snippets.

@ogwurujohnson
Created October 21, 2020 17:42
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 ogwurujohnson/704eef6107e1706ace91ee27fc6d0d9d to your computer and use it in GitHub Desktop.
Save ogwurujohnson/704eef6107e1706ace91ee27fc6d0d9d to your computer and use it in GitHub Desktop.
Reimplement lodash groupby
const users = [
{ name: 'Jim', color: 'blue' },
{ name: 'Sam', color: 'blue' },
{ name: 'Eddie', color: 'green' },
];
const usersByColor = users.reduce((acc, value) => {
if (!acc[value.color]) {
acc[value.color] = [];
}
acc[value.color].push(value);
return acc;
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment