2

The following is data in array (id,num1,num2,qty).

1,500,1000,1
2,700,1200,1
3,900,1400,1

How do I sum and display the following data?

500 + 700 + 900
1000 + 1200 + 1400

Result 2100 3600

This is what I have so far...

var allItems = [];

function calcWattage() {
 allItems = [];
 $(".cbx").each(function(){
  if($(this).is(":checked"))
      allItems.push(this.id + "," + ($(this).val()) + "<br />");
 });
 $("#result").html(allItems.join(""));
}
0

1 Answer 1

3

You can use Array.reduce() like

var array = [
  [1, 500, 1000, 1],
  [2, 700, 1200, 1],
  [3, 900, 1400, 1]
];

var result = array.reduce(function(value, array) {
  value[0] += array[1];
  value[1] += array[2];
  return value;
}, [0, 0]);
console.log(result)

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

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.