Skip to content
Advertisement

Take on Array of Objects, Find Nested Array in Object, Calculate TotalAmount and Add Back to Original Object

This is by far one of the more complicated tasks I’ve had to do with objects, and I’m stuck on how to do this, or if it is even possible.

Let’s say I have an array of objects that looks like this –

[
                {
                    "id": "5555",
                    "title": "Invoice",
                    "amount": 2,
                    "address": "12",
                    "items": [
                        {
                            "title": "foobar",
                            "amount": 2,
                            "quantity": 1,
                            "discount": 0,
                            "taxes": [
                                {
                                    "rate": 10,
                                    "name": "tax"
                                }
                             ]
                        }
                    ]
                }
]

I need to take this object. Multiply the amount * quantity, then multiply this by the tax. So (amount * quantity) * tax. Then when I have this answer add it back to the original object like so –

[
                {
                    "id": "5555",
                    "title": "Invoice",
                    "amount": 2,
                    "address": "12",
                    "items": [
                        {
                         "title": "foobar",
                         "amount": "2.2",
                         "tax": ".2"
                      }
                    ]
                }
]

I believe the way to do this is with the .map() function, however I’m struggling with how to get A. How to calculate this and add it back to the original amount to get the total amount. B. Once I have this add the completed total amount how to add the new Items array back into the original object.

What I have so far and where I got stuck –

var initialArr = input.testArr; 

const newArr = initialArr.map(invoice => {

  return invoice.items;
  
});

const calcArr = newArr.map(items => {
 
  return {
    totalAmount :  (newArr.amount * newArr.quantity)
  }
  
});

  console.log(calcArr);

totalAmount is coming up as null, and once I have that total amount I still need to have the amount multiplied by the taxrate and then have that added back to the totalAmount then have this completed object added back in.

Advertisement

Answer

Looks like it should be relatively straightforward. Iterate over every object and reassign its items array property with .map. Take the amount, title, and taxes from each subitem, calculate the tax amount by flattening all taxes in the taxes array, then return a new object with the tax amount, and the tax amount added to the original amount:

const input = [
                {
                    "id": "5555",
                    "title": "Invoice",
                    "amount": 2,
                    "address": "12",
                    "items": [
                        {
                            "title": "foobar",
                            "amount": 2,
                            "quantity": 1,
                            "discount": 0,
                            "taxes": [
                                {
                                    "rate": 10,
                                    "name": "tax"
                                }
                             ]
                        }
                    ]
                }
];

for (const obj of input) {
  obj.items = obj.items.map(({
    amount,
    title,
    taxes
  }) => {
    const totalTaxPercent = taxes.reduce((a, b) => a + b.rate, 0);
    const tax = (totalTaxPercent / 100) * amount;
    return {
      title,
      amount: String(amount + tax),
      tax: String(tax),
    };
  });
}
console.log(input);
Advertisement