MacD MacD - 4 years ago 106
AngularJS Question

Simple sum ng-repeat column

I have a page that returns a list from an ng-repeat. All I need to do is add up the numbers that are returned. I know there is a simple solution. However, I cannot find the correct implementation.

Html:

<ion-list class="item-borderless">
<div class="card" ng-repeat="product in productsToFilter">
<div class="row">
{{product.shareCount}}
</div>
</div>
</ion-list>


Javascript:

$scope.productsToFilter
$scope.productsToFilter = [];

firebase.database().ref('products').once('value', function(products) {
products.forEach(function(product) {
var product = {
shareCount: product.val().shareCount
}
}
};


Returns a list of numbers, and all I'm looking for is the total of all of them regardless of how many records there are.

Answer Source

As you are already iteration over products array via forEach, then I think you can just add them to a variable to get the total value of shareCount.

var totalCount = 0;
products.forEach(function(product) {
      totalCount += product.val().shareCount;
      var product = {
        shareCount: product.val().shareCount
      }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download