-1

JSON:(Json Looks like below, first I need to groupBy title and based on that record need to groupby month using createdAt property and based on month values need to add recieved_qty property.. For Example, In product_id object we have 2 formula1 titles need to groupBy both records, after this groupby based on month, finally add receicved qty from final record..can you help me to do this? )

[
   {
      "createdAt": "2020-03-04T10:39:46.000Z",
      "updatedAt": "2020-03-12T07:23:42.000Z",
      "id": 8,
      "company_id": 5,
      "user_id": 51,
      "recieved_qty": 10,
      "prev_received_qty": 0,
      "status": "Active",
      "mlc_id": 3,
      "product_id": {
         "createdAt": "2020-03-04T10:35:33.000Z",
         "updatedAt": "2020-03-12T07:23:42.000Z",
         "id": 7,
         "company_id": 5,
         "user_id": 51,
         "title": "Formula1",
         "batch_qty": 10,
         "allergen": [
            "Egg"
         ],
      },
      "mlc_packaging_id": 3
   },
   {
      "createdAt": "2020-03-04T10:35:41.000Z",
      "updatedAt": "2020-03-04T10:35:41.000Z",
      "id": 9,
      "company_id": 5,
      "user_id": 51,
      "recieved_qty": 10,
      "prev_received_qty": 0,
      "status": "Active",
      "mlc_id": 3,
      "product_id": {
         "createdAt": "2020-03-04T10:35:33.000Z",
         "updatedAt": "2020-03-12T07:23:42.000Z",
         "id": 7,
         "company_id": 5,
         "user_id": 51,
         "internalCode": "FO-0001",
         "myCode": "FO-4502",
         "title": "Formula1",
         "batch_qty": 10,
         "allergen": [
            "Egg"
         ],

      },
      "mlc_packaging_id": 3
   },
   {
      "createdAt": "2020-03-04T09:41:54.000Z",
      "updatedAt": "2020-03-04T10:10:01.000Z",
      "id": 10,
      "company_id": 5,
      "user_id": 51,
      "recieved_qty": 10,
      "prev_received_qty": 5,
      "status": "Active",
      "mlc_id": 4,
      "product_id": {
         "createdAt": "2020-03-04T10:38:15.000Z",
         "updatedAt": "2020-03-04T09:58:48.000Z",
         "id": 11,
         "company_id": 5,
         "user_id": 51,
         "internalCode": "FO-0005",
         "myCode": "FO-4258",
         "title": "NewFormula",
         "batch_qty": 10,
         "allergen": [
            "Egg"
         ],

      },
      "mlc_packaging_id": 4
   },
   {
      "createdAt": "2020-03-04T10:11:09.000Z",
      "updatedAt": "2020-03-04T10:11:09.000Z",
      "id": 11,
      "company_id": 5,
      "user_id": 51,
      "recieved_qty": 1000,
      "prev_received_qty": 0,
       "status": "Active",
      "mlc_id": 3,
      "product_id": {
         "createdAt": "2020-03-04T10:35:33.000Z",
         "updatedAt": "2020-03-12T07:23:42.000Z",
         "id": 7,
         "company_id": 5,
         "user_id": 51,
         "title": "Formula1",
         "batch_qty": 10,
         "allergen": [
            "Egg"
         ],

      },
      "mlc_packaging_id": 3
   }
]
Rekha
  • 433
  • 7
  • 22

1 Answers1

1
var grouped = sourceArray.reduce(function(prev, current) {
  var title = current.product_id.title;
  var month = current.createdAt.substr(0, 7);
  var key = month + title;

  var item = prev[key];
  if (!item) {
    item = {
      title: title,
      month: month
    };
    prev[key] = item;
  }
  item.recieved_qty = (item.recieved_qty || 0) + current.recieved_qty;
  return prev;
}, {});
var result = Object.values(grouped);

result is then an array where each element is an object like

{title: "Some title", month: "2020-02", recieved_qty: 33 }
Christian
  • 1,250
  • 9
  • 12
  • One thing, Date is not getting proper format.. current.createdAt.substr(0, 7) => this line gives substr of undefined error.. so i changed String(current.createdAt).substr(0, 7); then error gone.. but output is month:"wed Mar".. – Rekha Mar 17 '20 at 09:26
  • 1
    This might happen because `createdAt` might be a date instead of a string (as it is in the JSON you posted in your question). In this case you can convert that date to a string using `createdAt.toISOString().substr(0, 7)` – Christian Mar 17 '20 at 11:01