JavaScript中的Firebase对象值的Sum数组

时间:2017-02-05 01:07:59

标签: javascript arrays firebase ionic-framework firebase-realtime-database

我有一个对象数组,我希望对每个对象值求和。我试图从以下post实施reduce()forEach(),但无济于事。

如何返回newProducts var?

中每个键的总和
$scope.products = [];
      var userId = firebase.auth().currentUser.uid;
      firebase.database().ref('accounts').orderByChild('userId').equalTo(userId).once('value').then(function(accounts) {
        if (accounts.exists()) {
          accounts.forEach(function(account) {
            $scope.accountId = account.key;
            $scope.productIds = account.val().products;
            if (!$scope.productIds) {
              //User has no products yet!
              Utils.hide();
            }
            //Fetch each products the user have given the productIds
            $scope.productIds.forEach(function(productId) {
              //Get the product.
              firebase.database().ref('products/' + productId).once('value', function(product) {

                //Create product object to be added to the productList.
                var newProducts = {
                  id: productId,
                  name: product.val().name,
                  shareCount: product.val().shareCount,
                  viewCount: product.val().viewCount
                };

                //Add product to productList.
                $scope.products.push(product);
                $scope.$apply();
                Utils.hide();

              });
            });
          });
        }
      });

提前致谢!

0 个答案:

没有答案