I discovered _.reduce however it only keeps 1 of set of information. What option should be used to combine like information?
[
{
fruit: 'apple',
'color': 'red'
},
{
fruit: 'apple',
'color': 'green'
},
{
fruit: 'pear',
'color': 'yellow'
},
{
fruit: 'pear',
'color': 'green'
}
]
My desired outcome would be:
var items = [
{
fruit: 'apple',
'color': 'red', 'green'
},
{
fruit: 'pear',
'color': 'green', 'yellow'
}
]
The current code I have using NodeJS is:
let result = Object.values(json.reduce((c, {fruit,...r}) => {
c[fruit] = c[fruit] || {fruit};
c[fruit] = Object.assign(c[fruit], r);
return c;
}, {}));
_.groupBy. You may need an additional post processing step to achieve your final data structure, but_.groupBydoes a lot of the work.