I don't know how to really reduce the method, I tried to use this to obtain the next JSON, but it obviously did not work.
{
"w_elem": [
{
"item": [
{"type": "type_a","price": "100","discount": "10"},
{"type": "type_b","price": "120","discount": "12"},
{"type": "type_a","price": "165","discount": "10"}
],
"user": "1001",
"userType": "A"
},
{
"item": [
{"type": "type_a","price": "85","discount": "5"},
{"type": "type_a","price": "15","discount": "0"}
],
"user": "1002",
"userType": "B"
}
]
}```
w_elem.forEach(elem => {
let user= elem.user;
let aa = Object.values(elem.item.reduce((acc, {type, price, discount}) => {
acc[type] = acc[type] || {type, price: 0, discount: 0, Count: 0};
acc[type].price+= Number(price);
acc[type].discount+= Number(discount);
acc[type].Count += 1;
return acc;
},{}));
});```
i tried to do the next
{
"user": "1001",
"item": [
{"type": "type_a","price": "265","discount": "20"},
{"type": "type_b","price": "120","discount": "12"},
]
},
{
"user": "1002",
"item": [
{"type": "type_a","price": "85","discount": "5"},
{"type": "type_b","price": "15","discount": "0"}
]
}
]
i dont understand how mantain the struct and only group items by type using reduce