0

I'm having some trouble calculating subtotals in a table. Basically I have 3 columns of variables; FRUIT, ORIGIN, and PRICE. FRUIT contains things like "Apple", "Banana", "Orange", etc. I want to populate a table that shows the total price of the FRUIT column clustered by the values in the ORIGIN column. I may be overlooking an easier way of doing this, but here's what I've tried.

  for(i=0;i<FRUIT.length;i++)
  {
  if(ORIGIN[i] == "Africa")
  {
  SUBTOTAL.push(PRICE[i])
  }
  }

I then use the following method to allow me place SUBTOTAL.sum() in my table to show the total price of fruit from Africa:

  Array.prototype.sum = function() {
  for (var j = 0, L = this.length, sum = 0; j < L; sum += this[j++]);
  return sum;

The sum method works properly on the full column (for example PRICE.sum()) but I get incorrect subtotals using my current if statement. It also throws off the figures in my table which were previously correct before I added it, such as PRICE.sum().

Any recommendations would be greatly appreciated!

Many Thanks, Karl

4

0 回答 0