What is the best way to refactor(ise) an array / array of objects ? I was thinking of using array.reduce, but could not figure out a solution.
for example, transform this array :
const carsInput =
[
{
brand: 'volkswagen',
model: 'golf',
motorSpec: {
...
},
Dim: {
...
},
},
...
{
brand: 'volkswagen',
model: 'passat',
motorSpec: {
...
},
Dim: {
...
},
},
]
to this array of objects
const carsOutput =
{
volkswagen: {
brandName: 'volkswagen',
models: {
golf: {
modelName: 'golf',
motorSpec: {
...
},
Dim: {
...
},
},
passat: {
modelName: 'passat',
motorSpec: {
...
},
Dim: {
...
},
},
},
},
}
Also if you think of another (better) way of presenting this array of objects, I am all ears ! Thanks a lot
I am sure others have more concise ways of doing this but here is one approach.