I have a collection of objects organized by index and I want to rearrange them while adding categories to the output.
This is the initial collection:
let vehicles = [{'make': 'audi', 'model': 'RS3', 'transmition': 'automatic'}, {'make': 'audi', 'model': 'RS7', 'transmition': 'dual-clutch'}, {'make': 'bmw', 'model': '325is', 'transmition': 'manual'}, {'make': 'bmw', 'model': 'M2', 'transmition': 'dual-clutch'}]
Shown below is the code used to group the items by make:
var groupedByMake = _.groupBy(
vehicles,
"make"
);
The resulting categorization is as follows:
{
'audi':[{'model': 'RS3', 'transmition': 'automatic'}, {'model': 'RS7', 'transmition': 'dual-clutch'}],
'bmw':[{'model': '325is', 'transmition': 'manual'}, {'model': 'M2', 'transmition': 'dual-clutch'}]
}
My objective is to format the outcome like this:
[{
'make': 'audi',
'types': [{'model': 'RS3', 'transmition': 'automatic'}, {'model': 'RS7', 'transmition': 'dual-clutch'}]
},{
'make': 'bmw',
'types': [{'model': '325is', 'transmition': 'manual'}, {'model': 'M2', 'transmition': 'dual-clutch'}]
}]
If it's doable using JavaScript, could someone help me achieve this? Thank you.