0

I have an array of objects.

Objects looks like this:

0: Object
_id: "555baa9a034de05173e9fee4"
name: "Smirnoff Vodka"
number: 120
priceQuantity: 195
subtotal: 195
taxes: Array[0]

Now, from all the objects, I have to calculate sum of amount of total sale of every time so that I know the total sale amount of every item. .

i.e push the values in these arrays

itemAmount=[];
itemName=[];

The array is going to be very large therefore I need it to be very efficient at the same time.

Also, before the total amount is calculated, I have to iterate over the taxes array so that to add all the taxes. What could be the efficient way to do it?

10
  • 1
    What inefficient way have you found? Commented Aug 13, 2015 at 10:41
  • 1
    Define "very large". Commented Aug 13, 2015 at 10:41
  • what toal sale is it price Commented Aug 13, 2015 at 10:43
  • Having two arrays for name and amount doesn't make much sense to me either. Don't you want those two pieces of information linked? Commented Aug 13, 2015 at 10:44
  • 2
    @SimranKaur I think showing your code is still a good idea. Even though it seems not satisfactory to you, you might be surprised Commented Aug 13, 2015 at 10:46

3 Answers 3

1

You can try something like this:

var itemAmount = [], itemName = [];

myArray.map(function(obj){
  var total = obj.subtotal;
  for (var i = 0; i < obj.taxes.length; i++) {
      total += obj.taxes[i];
  }

  var index = itemName.indexOf(obj.name);
  if (index === -1) {
    itemAmount.push(total);
    itemName.push(obj.name);
  } else {
    itemAmount[index] += total;
  }
});

demo

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

4 Comments

Oh, That did not help. It is pushing elements with repeating elements over again . I do not want that.
if the prices of any products change, the calculated totals will be wrong.
@Pjetr Why would we recalculate the totals if the price of the products are changing.. The amount of money which the company earned was calculated at the previous price, before the price changes.. If you recalculate the totals then, for sure, you will have wrong data about what you sale at what price..
@MateiMihai, because of the nature of Javascript which calculates at runtime, using the values it has at the present. So unless you save this data somewhere, you'll recalculate this every time you run the application. And if you store the values, why would you need to calculate this? Just every time you sell an item you should update the stored value, using the current price. If it's simply to store a value with the current data, because this is a new feature, there are better ways to calculate the value without using JS. That's why I make the assumption that OP will always recalculate it…
0

If you need these values every time you open your application, it might be a good idea to not calculate these values, but to store them in your database. For every purchase, just adjust the totalSale value with the current price, and update totalSaleVAT with current price and VAT.

In addition to being a lot easier, doing it this way will give no false data when the prices change. Because the sold values won't be affected when the price rises or falls.

Comments

0

why dont you use the good things of javascript like dinamic allocation but the bad ones?

I would set an object for the products:

var Products = {}

And then set the entries like:

if(!products[WHATEVER])
  products[WHATEVER] = [];

var temp = {};
temp.name = "asdf";
temp....

products[WHATEVER].push(temp);

so you have one products object where you can access just using id, name or whatever you want to any product, which is an array of the product entries.

Try it

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.