I'm having an issue with the following code :
function getTotalChildAttr2(parent,child) {
var res = 0;
for (var i = 0, iMax = 2; i < iMax; i++) {
angular.forEach(vm.filteredRoutersSummaries[i], function (summary) {
res += summary[parent][child];
});
}
console.log(vm.filteredRoutersSummaries);
return res;
}
Basically the problem with this is that res adds up with every vm.filteredRoutersSummaries due to [i], and then returns res, what i would like is that res is returned for every vm.filteredRoutersSummaries. for exemple (vm.filteredRoutersSummaries is an array, but let's imagine it's just an integer) if i have :
vm.filteredRoutersSummaries[0] = 10
vm.filteredRoutersSummaries[1] = 15
currently, i would get returned res = 25, well what i want is : res = 10 with vm.filteredRoutersSummaries[0] and res = 15 with vm.filteredRoutersSummaries[1]. ( because later i do for exemple :
vm.routers[i].value = getTotalChildAttr2('real', 'value');
I can make that happen by doing it in two functions (without using a for condition, and simply replace i by 0 in vm.filteredRoutersSummaries in the angular.foreach in the 1st function, and by 1 in the 2nd function. I don't really know if that's very clear, if you have any questions please don't mind asking me ! TY !
getTotalChildAttr2('real', 'value', i);and then resolve the answer only forvm.filteredRoutersSummaries[i]. Maybe something like:vm.filteredRoutersSummaries[i].reduce((x,y)=>{return x+y[parent][child]},0)