Is there an easy way to aggregate an array of objects based on a specific property in JavaScript?
[ { key: 'black', value: [ '2', '3', '9' ] },
{ key: 'black', value: [ '1' ] },
{ key: 'gold', value: [ '2', '3' ] },
{ key: 'gold', value: [ '1' ] },
{ key: 'red', value: [ '9' ] },
{ key: 'white', value: [ '2', '3' ] },
{ key: 'white', value: [ '1' ] } ]
I want to transform the above array into:
[ { key: 'black', value: [ '1', '2', '3', '9' ] },
{ key: 'gold', value: [ '1', '2', '3' ] },
{ key: 'red', value: [ '9' ] },
{ key: 'white', value: [ '1', '2', '3' ] } ]
If anyone knows a simple solution using lodash or Array.reduce, I would greatly appreciate the help!