AngularJS forEach并添加值

时间:2014-06-04 11:03:44

标签: javascript angularjs

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

我有上述内容,我正在尝试获得赠款总额,似乎为我的grandTotal

返回NaN

更新:

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));

这将返回enter image description here

1 个答案:

答案 0 :(得分:1)

如何使用vanilla javascript mapreduce函数?

基于您的目的的例子:http://jsfiddle.net/optyler/G2ZJa/