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.
CodePudding user response:
use reduce
method:
const myArr = [{
"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",
}
}]
let results = myArr.reduce(function(results, newArr){
(results[newArr.group.id] = results[newArr.group.id] || []).push(newArr);
console.log(results);
},{})
less code and better result
CodePudding user response:
Let's call your object inputArr
You will first need to run a reduce on it to group it by groupId:
const intermediaryResult = inputArr.reduce((groups, current) => {
groups[current.group.id] = {
...groups[current.group.id],
id: current.group.id,
name: current.group.name,
[current.class]: current.price
}
return groups;
}, {})
Then, you'll be left with something like
{
"group-1": {
"id": "group-1",
"name": "Special",
"audi": 10,
"mercedess": 20,
"bmw": 30
},
"group-2": {
"id": "group-2",
"name": "Normal",
"audi": 50,
"mercedess": 60,
"bmw": 70
}
}
Which can easily be transformed in what you'd like with
Object.values(intermediaryResult)
All in all
Object.values(
inputArr.reduce((groups, current) => {
groups[current.group.id] = {
...groups[current.group.id],
id: current.group.id,
name: current.group.name,
[current.class]: current.price
}
return groups;
}, {})
)