Created
April 10, 2020 12:45
-
-
Save nkhil/5ca4490d887b01b672c21dff915e7bab to your computer and use it in GitHub Desktop.
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(key) { | |
return function group(array) { | |
return array.reduce((acc, obj) => { | |
const property = obj[key]; | |
acc[property] = acc[property] || []; | |
acc[property].push(obj); | |
return acc; | |
}, {}); | |
}; | |
} | |
const groupByYear = groupBy("year"); | |
groupByyear(albums); | |
const groupByArtist = groupBy("artist"); | |
groupByArtist(albums); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment