I have the following JavaScript object:
var example = [{
country: "US",
things: {
weather: 'cloudy'
}
},
{
country: "US",
things: {
resource: 'lead',
weather: 'sunny'
}
},
{
country: "MX",
things: {
weather: 'sunny'
}
},
{
country: "MX",
things: {
resource: 'gold',
weather: 'sunny'
}
},
{
country: "MX",
things: {
resource: 'copper'
}
},
]
I would like to convert to this format via aggregation.
var out = [{
country_code: 'US',
things: {
resource: ['lead'],
weather: ['cloudy', 'sunny']
}
},
{
country_code: 'MX',
things: {
resource: ['gold', 'copper'],
weather: ['sunny'],
}
}
]
I have tried to look into using combinations of reduce and map to no avail. It would be great if this example can also serve as a jumping off point for general strategies for data manipulation that may or may not involve the use of the array methods.