Created
August 21, 2018 20:33
-
-
Save mminer/c24f0586ae5956ee02e2fc955d840767 to your computer and use it in GitHub Desktop.
Groups an array of objects by a common key.
This file contains 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
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