skip to Main Content

I have an object that looks like as follows:

[
    {
        "Net_Amount": 499,
        "Date": "2022-01-09T18:30:00.000Z",
        "Scheme_Name": "CUSTOMERWINBACKJCA01",
        "Month": "Jan"
    },
    {
        "Net_Amount": 902,
        "Date": "2022-01-09T18:30:00.000Z",
        "Scheme_Name": "CUSTOMERWINBACKJCA02",
        "Month": "Jan"
    },
    {
        "Net_Amount": 1860,
        "Date": "2022-10-01T18:30:00.000Z",
        "Scheme_Name": "CUSTOMERCONNECTJCA",
        "Month": "Oct"
    },
    {
        "Net_Amount": 1889,
        "Date": "2022-11-01T18:30:00.000Z",
        "Scheme_Name": "CUSTOMERCONNECTJCA",
        "Month": "Nov"
    }
]

Now, if you will look carefully, I have a common field Month in the objects and I want merge the objects based on this common field only. How I want my object to be formatted is as :

[
    {
        "Month": "Jan",
        "varData":  [{
            "Net_Amount": 499,
            "Date": "2022-01-09T18:30:00.000Z",
            "Scheme_Name": "CUSTOMERWINBACKJCA01"
        },
        {
            "Net_Amount": 902,
            "Date": "2022-01-09T18:30:00.000Z",
            "Scheme_Name": "CUSTOMERWINBACKJCA02"
        }]
    },
    {
        "Month": "Oct",
        "varData":  [{
            "Net_Amount": 1860,
            "Date": "2022-10-01T18:30:00.000Z",
            "Scheme_Name": "CUSTOMERCONNECTJCA"
        }]
    },
    {
        "Month": "Nov",
        "varData":  [{
            "Net_Amount": 1889,
            "Date": "2022-11-01T18:30:00.000Z",
            "Scheme_Name": "CUSTOMERCONNECTJCA"
        }]
    }
]

I can do it by iterating over the array and checking if month is same, then pushing the other key and its value of object in the varData but I want to know if there is any shortcut or inbuilt function which I can use to achieve my purpose.

3

Answers


  1. I don’t think that there is some better built-in solution then iterating the array.

    But if you use month names as keys then the code could be quite straightforward (the output is not exactly the same but quite similarly structured).

    const result = {}
    for (const entry of list) {
      if (!result[entry.Month]) {
         result[entry.Month] = []
      }
      result[entry.Month].push(entry)
    }
    

    See jsfiddle.

    If you need the output that is exactly specified in the question then you can use the following code:

    let result = {}
    for (const entry of list) {
      const month = entry.Month
      if (!result[month]) {
         result[month] = {
          "Month":   month,
          "varData": []
         }
      }
      delete entry.Month
      result[month].varData.push(entry)
    }
    result = Object.values(result)
    

    See jsfiddle

    Login or Signup to reply.
  2. const data = [{"Net_Amount":499,"Date":"2022-01-09T18:30:00.000Z","Scheme_Name":"CUSTOMERWINBACKJCA01","Month":"Jan"},{"Net_Amount":902,"Date":"2022-01-09T18:30:00.000Z","Scheme_Name":"CUSTOMERWINBACKJCA02","Month":"Jan"},{"Net_Amount":1860,"Date":"2022-10-01T18:30:00.000Z","Scheme_Name":"CUSTOMERCONNECTJCA","Month":"Oct"},{"Net_Amount":1889,"Date":"2022-11-01T18:30:00.000Z","Scheme_Name":"CUSTOMERCONNECTJCA","Month":"Nov"}]
    
    console.log([...new Set(data.map(i=>i.Month))].map(Month=>
      ({Month, varData: data.filter(({Month:m})=>m===Month).map(({Month,...o})=>o)})))
    Login or Signup to reply.
  3. const dataArr = [
        {
            Net_Amount: 499,
            Date: "2022-01-09T18:30:00.000Z",
            Scheme_Name: "CUSTOMERWINBACKJCA01",
            Month: "Jan",
        },
        {
            Net_Amount: 902,
            Date: "2022-01-09T18:30:00.000Z",
            Scheme_Name: "CUSTOMERWINBACKJCA02",
            Month: "Jan",
        },
        {
            Net_Amount: 1860,
            Date: "2022-10-01T18:30:00.000Z",
            Scheme_Name: "CUSTOMERCONNECTJCA",
            Month: "Oct",
        },
        {
            Net_Amount: 1889,
            Date: "2022-11-01T18:30:00.000Z",
            Scheme_Name: "CUSTOMERCONNECTJCA",
            Month: "Nov",
        },
    ];
    
    const outputObj = dataArr.reduce((acc, crt) => {
        acc[crt.Month] ??= [];
        acc[crt.Month].push(crt);
        return acc;
    }, {});
    
    
    const outputArr = Object.values(outputObj).map((item) => ({ Month: item[0].Month, varData: item }));
    
    console.log(outputArr);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search