0
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

3
  • 2
    can you please post sample data for $scope.gear object Commented Jun 4, 2014 at 11:21
  • I guess your grandTotal variable was not initialized :) So grandTotal += rowTotal will result in nan. Use var grandTotal = 0; Commented Jun 4, 2014 at 11:51
  • @guru I have updated the Original Post with the results Commented Jun 4, 2014 at 11:57

1 Answer 1

1

What about using vanilla javascript map and reduce functions ?

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

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.