Javascript 在数组中添加值会打印值,而不会添加角度
我试图在数组中添加一些值,但它不是输出总和,而是将两个值相邻打印 我的阵列是:Javascript 在数组中添加值会打印值,而不会添加角度,javascript,angularjs,Javascript,Angularjs,我试图在数组中添加一些值,但它不是输出总和,而是将两个值相邻打印 我的阵列是: $scope.values = [ {amount: 5}, {amount: 5} ] 我的职能是: $scope.total = function() { var total = 0; angular.forEach($scope.values, function(item) { total += item.amount; }) return total;
$scope.values = [
{amount: 5},
{amount: 5}
]
我的职能是:
$scope.total = function() {
var total = 0;
angular.forEach($scope.values, function(item) {
total += item.amount;
})
return total;
}
当我调用{{amountemaining()}}
时,它显示“55”,而不是10
当我将另一个值为6的对象推送到数组时,它显示“556”
要补充的另一个注意事项是,当我调用{{values}}时,它将5放在“”内,我认为这是罪魁祸首
"amount":"05"
如何确保数字是整数?您应该显式强制它
total += Number(item.amount);
或
后者将努力从参数中提取任何有效的数字,第一个将检查它是否是一个数字,并解析它或是NaN。您应该显式强制它
total += Number(item.amount);
或
后者将努力从参数中提取任何有效的数字,第一个将检查它是否是一个数字,并解析它或是NaN