I have a similar structure as below:
$scope.traveler = [
{ description: 'Senior', Amount: 50},
{ description: 'Senior', Amount: 50},
{ description: 'Adult', Amount: 75},
{ description: 'Child', Amount: 35},
{ description: 'Infant', Amount: 25 },
];
To calculate the total amount of this array, I currently use the following method:
$scope.totalAmount = function(){
var total = 0;
for (var i = 0; i < $scope.traveler.length; i++) {
total = total + $scope.traveler[i].Amount;
}
return total;
}
While this works fine for one array, I have other arrays with different property names that I need to sum up as well.
It would be more convenient if I could achieve the same result using a method like this:
$scope.traveler.Sum({ Amount });
However, I'm unsure about how to implement this in a generic way that can be reused for arrays with different property names such as:
$scope.someArray.Sum({ someProperty });