0

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.

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

1 Comment

with a slight modification of this I got exactly what I needed. just needed another const for subTotal which would be amount * quantity. I'm starting to understand how the .Map() method works, the documentation everywhere I read is so confusing. I usually don't have that much trouble learning a new method. Anyways thank you for your assistance, it's much appreciated.
1

Decompose it into two smaller problems. First, you described the cost of an item pretty well, though I assume from the data that you want to sum the taxes. The rest is just an application of reduce on items in the invoice...

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

function costOfItem(item) {
  const totalTax = item.taxes.reduce((acc, el) => acc + el.rate, 0)
  return (item.quantity * (item.amount - item.discount)) * (1 + totalTax / 100)
}

function costOfInvoice(invoice) {
  return invoice.items.reduce((acc, item) => acc + costOfItem(item), 0);
}

const costs = input.map(invoice => ({ title: invoice.title, cost: costOfInvoice(invoice) }))

console.log(costs)

Comments

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.