在循环中无法获得总和 [英] Can't get the summation in for loop
本文介绍了在循环中无法获得总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 for循环
中有一个角度服务
,它返回一个对象数组。我想获得由 service
返回的值的总和,但是我最终没有得到任何结果。我的服务工作正常,但我的问题是我无法得到总结。下面是我的代码
控制器
var TotalOBValueLand = 0; (var i = 0; i <$ scope.selectedProp.length; i ++){
AccountService.getTopAccountDetails($ scope.selectedProp [i] [propId])。 msg){
TotalOBValueLand + = parseInt(msg.data [0] .OBValueLand);
//我在这里返回的数据没有错误
});
}
console.log(TotalOBValueLand); //我有零;
解决方案使用
Promise.all
和数组#map
得到一个结果数组,然后使用Array#reduce将它们加起来 var TotalOBValueLand = 0;
Promise.all($ scope.selectedProp.map(function(prop){
return AccountService.getTopAccountDetails(prop).then(function(msg){
return parseInt(msg.data [ 0).OBValueLand);
});
}))then(function(results){
TotalOBValueLand = results.reduce(function(a,b){
return a + b;
});
console.log(TotalOBValueLand);
});
回复评论
var TotalOBValueLand = 0;
var TotalOBValueBuilding = 0;
Promise.all($ scope.selectedProp.map(function(prop){
return AccountService.getTopAccountDetails(prop).then(function(msg){
return parseInt(msg.data [ 0));
});
}))。then(function(results){
TotalOBValueLand = results.reduce(function(a,b){
return a。 OBValueLand + b.OBValueLand;
));
TotalOBValueBuilding = results.reduce(function(a,b){
return a.OBValueBuilding + b.OBValueBuilding;
});
console.log(TotalOBValueLand,TotalOBValueBuilding);
});
更通用一些
Promise.all($ scope.selectedProp.map(function(prop){
return AccountService.getTopAccountDetails(prop) .then(function(msg){
return parseInt(msg.data [0]);
});
}))。then(function(results){
var totalals = results.reduce(function(result,a){
Object.keys(a).forEach(function(key){
result [key] =(result [key] || 0)+一个[键];
));
返回结果;
},{});
console.log(totals.OBValueLand,totals.OBValueBuilding);
});
I have a angular service
inside a for loop
that returns an array of object. I want to get the summation of the value returned by that service
but I got nothing in the end. My service works fine but my problem is I can't get the summation. Below is my code
controller
var TotalOBValueLand = 0;
for(var i = 0; i < $scope.selectedProp.length; i++){
AccountService.getTopAccountDetails($scope.selectedProp[i]["propId"]).then(function(msg){
TotalOBValueLand += parseInt(msg.data[0].OBValueLand);
//my return data here has no error.
});
}
console.log(TotalOBValueLand); //I got zero;
解决方案
Use Promise.all
and array#map
to get an array of results, then use Array#reduce to sum them up
var TotalOBValueLand = 0;
Promise.all($scope.selectedProp.map(function(prop) {
return AccountService.getTopAccountDetails(prop).then(function(msg){
return parseInt(msg.data[0].OBValueLand);
});
})).then(function(results) {
TotalOBValueLand = results.reduce(function(a, b) {
return a + b;
});
console.log(TotalOBValueLand);
});
In response to the comments
var TotalOBValueLand = 0;
var TotalOBValueBuilding = 0;
Promise.all($scope.selectedProp.map(function(prop) {
return AccountService.getTopAccountDetails(prop).then(function(msg){
return parseInt(msg.data[0]);
});
})).then(function(results) {
TotalOBValueLand = results.reduce(function(a, b) {
return a.OBValueLand + b.OBValueLand;
});
TotalOBValueBuilding = results.reduce(function(a, b) {
return a.OBValueBuilding + b.OBValueBuilding ;
});
console.log(TotalOBValueLand, TotalOBValueBuilding);
});
and a little more generic
Promise.all($scope.selectedProp.map(function(prop) {
return AccountService.getTopAccountDetails(prop).then(function(msg){
return parseInt(msg.data[0]);
});
})).then(function(results) {
var totals = results.reduce(function(result, a) {
Object.keys(a).forEach(function(key) {
result[key] = (result[key] || 0) + a[key];
});
return result;
}, {});
console.log(totals.OBValueLand, totals.OBValueBuilding);
});
这篇关于在循环中无法获得总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文