I have a JavaScript object as below:
[{
"id": "41e4858d-0478-4ffa-b39a-73db6f80c493",
"price": 10,
"class": "audi",
"group": {
"id": "group-1",
"name": "Special",
}
}, {
"id": "d4ed6efe-3d95-4f19-9f34-16b1d62ee5f1",
"price": 20,
"class": "mercedess",
"group": {
"id": "group-1",
"name": "Special",
}
}, {
"id": "a17a3671-09c4-411c-921f-a35705e86030",
"price": 30,
"class": "bmw",
"group": {
"id": "group-1",
"name": "Special",
}
}, {
"id": "1bda1fe8-c326-46f4-851c-d06c686d1ebc",
"price": 50,
"class": "audi",
"group": {
"id": "group-2",
"name": "Normal",
}
}, {
"id": "72300db9-c7f8-4bd6-af4a-f041e085bc6b",
"price": 60,
"class": "mercedess",
"group": {
"id": "group-2",
"name": "Normal",
}
}, {
"id": "3fd3a054-3f97-4899-ba28-e1f22ba40bc8",
"price": 70,
"class": "bmw",
"group": {
"id": "group-2",
"name": "Normal",
}
}]
I'm trying to group these objects by group id.
How can I achieve my desired output? Any help would be appreciated.
My desired output:
[
{
"id": "group-1",
"name": "special",
"mercedess": 20,
"bmw":30,
"auid":10
},
{
"id": "group-2",
"name": "normal",
"mercedess": 60,
"bmw":70,
"auid":50
}
]
Thank you very much to those who helped.
I tried to write it with JavaScript reduce
function or lodash
library, I couldn't get it to work.