1

I am having a double nested array of objects and I would like to change to a specific array of groups to support my table structure.

Below are my objects and the result I am expecting.

let newData = {
  data: {
    summary: [
      {
        code: "12",
        data: [
          {
            group: "Trade",
            currency: "CAD", //group1
            amount: "$3,443.00",
            total: 1
          },
          {
            group: "Trade",
            currency: "USD", //group2
            amount: "$0.00",
            total: 0
          },
          {
            group: "Commission",
            currency: "CAD", //group3
            amount: "$0.00",
            total: 0
          },
          {
            group: "Commission",
            currency: "USD", //group4
            amount: "$0.00",
            total: 0
          }
        ]
      },
      {
        code: "34",
        data: [
          {
            group: "Trade",
            currency: "CAD", //group1
            amount: "$343.00",
            total: 21
          },
          {
            group: "Trade",
            currency: "USD", //group2
            amount: "$40.00",
            total: 40
          },
          {
            group: "Commission",
            currency: "CAD", //group3
            amount: "$30.00",
            total: 30
          },
          {
            group: "Commission",
            currency: "USD", //group4
            amount: "$20.00",
            total: 20
          }
        ]
      }
    ]
  }
};



let result = [
{
    code: "12",
    TradeCAD:
    {
            group: "Trade",
            currency: "CAD", //group1
            amount: "$3,443.00",
            total: 1
          },
     TradeUSD:     
           {
            group: "Trade",
            currency: "USD", //group2
            amount: "$0.00",
            total: 0
          },
     CommissionCAD:     {
            group: "Commission",
            currency: "CAD", //group3
            amount: "$0.00",
            total: 0
          },
     CommissionUSD:     {
            group: "Commission",
            currency: "USD", //group4
            amount: "$0.00",
            total: 0
          }

       
  },
  {
    code: "34",
    TradeCAD:
    {
            group: "Trade",
            currency: "CAD", //group1
            amount: "$343.00",
            total: 21
          },
     TradeUSD:     
           {
            group: "Trade",
            currency: "USD", //group2
            amount: "$40.00",
            total: 40
          },
     CommissionCAD:     {
            group: "Commission",
            currency: "CAD", //group3
            amount: "$30.00",
            total: 30
          },
     CommissionUSD:     {
            group: "Commission",
            currency: "USD", //group4
            amount: "$20.00",
            total: 20
          }

       
  },
];

How I can iterate and change the whole object and If I do have a lot of data which is the best approach to follow to support fewer iterations?

1 Answer 1

2

try this, and see if work. you can condense the code into one function if you want to.

const result = newData.data.summary.map(item => {
  const res = {
    code: item.code,
  }
  item.data.forEach(d => {
    res[`${d.group}${d.currency}`] = d;
    d.amount = d.amount || "$0.00"; // default value for amount
    d.total = d.total || 0; // default value for total
  })
  return res;
});
console.log(JSON.stringify(result));
Sign up to request clarification or add additional context in comments.

8 Comments

It's working perfect thanks. Just a question.. how we can handle null check if the object is missing or any property is not there?
can you provide an example? :-)
if this object is missing in code: "34", { group: "Trade", currency: "CAD", //group1 amount: "$343.00", total: 21 },
or code: "34", missing group: "Trade" value
How we can check this kind of scenario?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.