ngplayground
ngplayground

Reputation: 21617

AngularJS forEach and adding values

var grandTotal;
angular.forEach($scope.gear, function(value, key) {
    var rowTotal = value.amount * value.price;
    console.log(rowTotal);
    grandTotal += rowTotal; 
});
console.log('grandTotal: '+grandTotal);

I have the above that I'm trying to get a grant total for, It seems to return NaN for my grandTotal

UPDATE:

var grandTotal = 0;
angular.forEach($scope.gear, function(value, key) {
    var rowTotal = value.amount * value.price;
    rowTotal = rowTotal.toFixed(1);
    console.log(rowTotal);
    grandTotal += rowTotal;
});
console.log('grandTotal: '+parseFloat(grandTotal));

this returns enter image description here

Upvotes: 0

Views: 514

Answers (1)

Patrick Ferreira
Patrick Ferreira

Reputation: 2053

What about using vanilla javascript map and reduce functions ?

An exemple based on your purpose here : http://jsfiddle.net/optyler/G2ZJa/

Upvotes: 1

Related Questions