What's the best way to transform flat data to a nested object grouped by different properties?
Our main concerns are about readability and maintainability.
Here is the data we have to transform :
const input = [
{
"ovenName": "Oven1",
"productCode": "P2",
"userId": 55,
"userName": "user1",
"quantity": 2
},
{
"ovenName": "Oven1",
"productCode": "P1",
"userId": 55,
"userName": "user1",
"quantity": 15
},
{
"ovenName": "Oven1",
"productCode": "P3",
"userId": 56,
"userName": "user2",
"quantity": 9
},
{
"ovenName": "Oven2",
"productCode": "P4",
"userId": 56,
"userName": "user2",
"quantity": 88
},
{
"ovenName": "Oven1",
"productCode": "P1",
"userId": 56,
"userName": "user2",
"quantity": 6
}
];
const wantedOutput = [
{
"oven": "Oven1",
"clients": [
{
"user":{
"name": "user1",
"id" : 55
},
"orders": [
{
"product": "P2",
"quantity": 2
},
{
"product": "P1",
"quantity": 15
}
]
},
{
"user":{
"name": "user2",
"id" : 56
},
"orders": [
{
"product": "P3",
"quantity": 9
},
{
"product": "P1",
"quantity": 6
}
]
}
]
},
{
"oven": "Oven2",
"clients": [
{
"user":{
"name": "user2",
"id" : 56
},
"orders": [
{
"product": "P4",
"quantity": 88
}
]
}
]
}
]
Our input is flat and we want to group by oven then by users and then by order (each product have a different order).
We already have a solution but we would like input on what direction to approach this issue (our solution is unreadable and we are looking for a better way)