Created
April 21, 2022 19:34
-
-
Save marceloglacial/39ace6c014badcf5d276653422261f91 to your computer and use it in GitHub Desktop.
groupArrayBy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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