3

I have an array below like this

[
  {
    "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
      }
    ]
  }
]

Here there is key Date and there are two values for the same date key. Now I want if Date is same then data array record should merged.

So i expect output as

[
  {
    "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
      }
    ]
  }
]

Please tell me best-optimized way of doing it ?

TechChain
  • 8,404
  • 29
  • 103
  • 228
  • Does this answer your question? [Group object values by date](https://stackoverflow.com/questions/62923017/group-object-values-by-date) – Yevhen Horbunkov Aug 20 '20 at 20:54

3 Answers3

1

You can use a dictionary and easily check if the key already exists and then append the new data, otherwise you will add the date as a new key.

var dict = {};
if (!("xxxx-xx" in dict)){ //if the key is not the dict

    dict["xxxx-xx"] = [{       //we are storing an array because we want to add other later
         id: "....",
         transactionId: "..."
         //etc...
    }]
}

else {                           //if the key already exists, we push new data to the array
    dict["xxxx-xx"].push({       //you can use push because is an array of objects
         id: "....",
         transactionId: "..."
         //etc...
    })
}

I hope that it can be helpful. An array would be less convenient to check if a key already exists and to avoid duplicates (by default with dict/object).

Marco Lampis
  • 403
  • 5
  • 15
  • Please check the expected output I want according to that. – TechChain Aug 21 '20 at 02:17
  • The date field Is redundant, of course if you really Need exactly that output this Is not a choice, but is Better for what you want to do. Instead of and object with "date and data" you have only one Key that identifies his data – Marco Lampis Aug 21 '20 at 08:21
  • This is a valid answer if output can't be changed https://stackoverflow.com/a/40775082/4757993 – Marco Lampis Aug 21 '20 at 08:26
0

arr = your data set

obj = this will be your output

arr.forEach((elem) => {
    if(obj[elem.Date]) {
       obj[elem.Date].data.push(elem.data);
    } else {
       obj[elem.Date] = {
       data: [elem.data]
    }
  }
});
0

First you have to group that array of objects by property Date. And iterate that grouped by key-value pairs of [Date, groupsByDate] then grab data from each group

const res = _.chain(data)
  .groupBy("Date")
  .toPairs()
  .map(([key, value]) => ({
    Date: key,
    data: _.flatMap(value, "data"),
  }))
  .value()

Full code

const data = [
  {
    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,
      },
    ],
  },
]

const res = _.chain(data)
  .groupBy("Date")
  .toPairs()
  .map(([key, value]) => ({
    Date: key,
    data: _.flatMap(value, "data"),
  }))
  .value()

console.log(JSON.stringify(res, null, 2))
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
hgb123
  • 13,869
  • 3
  • 20
  • 38