Skip to content

Instantly share code, notes, and snippets.

@marceloglacial
Created April 21, 2022 19:34
Show Gist options
  • Save marceloglacial/39ace6c014badcf5d276653422261f91 to your computer and use it in GitHub Desktop.
Save marceloglacial/39ace6c014badcf5d276653422261f91 to your computer and use it in GitHub Desktop.
groupArrayBy
// https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key
const groupArrayBy = (arr, prop) => {
return arr.reduce(function (groups, item) {
const val = item[prop]
groups[val] = groups[val] || []
groups[val].push(item)
return groups
}, {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment