Utilize the array.reduce() method for summing up the results. Here's an example:
$scope.totalOfSumAmount = $scope.items.reduce(function(carry, currentItem) {
//add the amount from the current item to the running total (carry)
return carry + currentItem.amount;
}, 0);//0 represents the initial value
The callback function (i.e.
function(carry, currentItem) {...
in the example) can actually accept four parameters:
- previous value - the value from the previous iteration (or the initial value)
- current value - the current element in the array
- current index - the current index of the current element in the array
- original array - the array being iterated over
The second parameter (optional) is the initial value, so if you need to start with a value other than 0, you can pass that instead.
angular.module('app', [])
.controller('ctrl', function($scope) {
$scope.items = [{
amount: 29.9,
code: "012",
currency: "BRL",
payedDate: "2016-11-10",
roDate: "2016-08-01",
type: "OTHER"
}, {
amount: 39.9,
code: "013",
currency: "BRL",
payedDate: "2016-11-11",
roDate: "2016-08-01",
type: "OTHER",
}];
$scope.totalOfSumAmount = $scope.items.reduce(function(carry, currentItem) {
//add the amount from the current item to the running total (carry)
return carry + currentItem.amount;
}, 0);//0 is the initial value
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
Total: <span> {{ totalOfSumAmount }} </span>
</div>