Reputation: 21
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(""));
}
Upvotes: 2
Views: 348
Reputation: 388316
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)
Upvotes: 3