Below is an array structure:
[
{
"Date": "2020-07",
"data": [
{
"id": "35ebd073-600c-4be4-a750-41c4be5ed24a",
"Date": "2020-07-03T00:00:00.000Z",
"transactionId": "13",
"transactionType": "Payment",
"amount": 1500
}
]
},
{
"Date": "2020-07",
"data": [
{
"id": "4e126519-e27b-4e82-bb81-689c7dc63c9b",
"Date": "2020-07-02T00:00:00.000Z",
"transactionId": "4",
"transactionType": "Payment",
"amount": 1000
}
]
},
{
"Date": "2020-06",
"data": [
{
"id": "646d6497-9dea-4f27-896e-a45d97a252ea",
"Date": "2020-06-04T00:00:00.000Z",
"transactionId": "14",
"transactionType": "Payment",
"amount": 1500
}
]
},
{
"Date": "2020-06",
"data": [
{
"id": "cf44e27f-2111-462d-b3bd-420a193745b8",
"Date": "2020-06-02T00:00:00.000Z",
"transactionId": "5",
"transactionType": "Payment",
"amount": 1000
}
]
}
]
The key Date
has multiple values for the same date. The goal is to merge data array records if the Dates are the same.
The expected output after merging is:
[
{
"Date": "2020-07",
"data": [
{
"id": "35ebd073-600c-4be4-a750-41c4be5ed24a",
"Date": "2020-07-03T00:00:00.000Z",
"transactionId": "13",
"transactionType": "Payment",
"amount": 1500
},
{
"id": "4e126519-e27b-4e82-bb81-689c7dc63c9b",
"Date": "2020-07-02T00:00:00.000Z",
"transactionId": "4",
"transactionType": "Payment",
"amount": 1000
}
]
},
{
"Date": "2020-06",
"data": [
{
"id": "646d6497-9dea-4f27-896e-a45d97a252ea",
"Date": "2020-06-04T00:00:00.000Z",
"transactionId": "14",
"transactionType": "Payment",
"amount": 1500
},
{
"id": "cf44e27f-2111-462d-b3bd-420a193745b8",
"Date": "2020-06-02T00:00:00.000Z",
"transactionId": "5",
"transactionType": "Payment",
"amount": 1000
}
]
}
]
What would be the most efficient way to achieve this?