I am working with a JSON response that contains multiple objects consisting of two main objects - datacenter and environment:
"deployments": [
{
"datacenter": {
"title": "euw1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "AAA",
"name": "foocluster",
"revision": "0",
"state": "Active",
}
},
{
"datacenter": {
"title": "apc1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "BBB",
"name": "foocluster",
"revision": "0",
"state": "Active"
}
},
{
"datacenter": {
"title": "euw1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "BBB",
"name": "foocluster",
"revision": "0",
"state": "Active"
}
},
{
"datacenter": {
"title": "use1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "AAA",
"name": "foocluster",
"revision": "0",
"state": "Active"
}
},
{
"datacenter": {
"title": "use2",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
"environment": {
"clusterId": "AAA",
"name": "foocluster",
"revision": "0",
"state": "Active"
}
},
]
I need to find the best way to group these objects by environment using JavaScript. The ideal result should look like this:
"deployments": [
{
"clusterId": "AAA",
"name": "foocluster",
"revision": "0",
"state": "Active",
"datacenters":[
{
"title": "euw1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
{
"title": "use1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
{
"title": "use2",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
]
},
{
"clusterId": "BBB",
"name": "foocluster",
"revision": "0",
"state": "Active",
"datacenters":[
{
"title": "euw1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
},
{
"title": "apc1",
"name": "foodatacenter",
"revision": "0",
"state": "Active"
}
]
}
]
Your assistance on how to achieve this would be greatly appreciated. Thank you for your help!