Skip to content

Instantly share code, notes, and snippets.

@nkhil
Created April 10, 2020 12:45
Show Gist options
  • Save nkhil/5ca4490d887b01b672c21dff915e7bab to your computer and use it in GitHub Desktop.
Save nkhil/5ca4490d887b01b672c21dff915e7bab to your computer and use it in GitHub Desktop.
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