Here's the input array which has multiple objects having key value pairs.
[
{
"countHouses": 1,
"Name": "Bob",
"Unique_ID": "12345"
},
{
"countFlats": 4,
"Name": "Bob",
"Unique_ID": "12345"
},
{
"countStadiums": 5,
"Name": "Efac",
"Unique_ID": "31124"
},
{
"countStadiums": 1,
"Name": "Bob",
"Unique_ID": "12345"
},
{
"countFlats": 1,
"Name": "Efac",
"Unique_ID__c": "31124"
},
{
"countHouses": 245,
"Name": "Efac",
"Unique_ID": "31124"
},
{
"countHouses": 300,
"Name": "Pox",
"Unique_ID": "18110"
}
]
The ask is to merge those objects into a single object within the array based on the Unique_ID field. Also, if there is only one object available, then that particular object should also be included with the other two count keys having value 0.
Final Response
[
{
"countHouses": 1,
"countFlats": 4,
"countStadiums": 1,
"Name": "Bob",
"Unique_ID": "12345"
},
{
"countHouses": 245,
"countFlats": 1,
"countStadiums": 5,
"Name": "Efac",
"Unique_ID": "31124"
},
{
"countHouses": 300,
"countFlats": 0,
"countStadiums": 0,
"Name": "Pox",
"Unique_ID": "18110"
}
]