Skip to content

Instantly share code, notes, and snippets.

@khaledosman
Created June 24, 2019 12:27
Show Gist options
  • Save khaledosman/68cd681c6576f0637a68b7e8b81580d8 to your computer and use it in GitHub Desktop.
Save khaledosman/68cd681c6576f0637a68b7e8b81580d8 to your computer and use it in GitHub Desktop.
group an array of objects by a key
const groupBy = (items, key) => items.reduce(
(result, item) => ({
...result,
[item[key]]: [
...(result[item[key]] || []),
item
]
}),
{}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment