我有一个对象数组,我希望对每个对象值求和。我试图从以下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();
});
});
});
}
});
提前致谢!