1

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 !

1
  • I think you need to pass the index: getTotalChildAttr2('real', 'value', i); and then resolve the answer only for vm.filteredRoutersSummaries[i]. Maybe something like: vm.filteredRoutersSummaries[i].reduce((x,y)=>{return x+y[parent][child]},0) Commented Feb 13, 2018 at 11:01

1 Answer 1

1

I hope i understood your problem, but sincerly it is not very clear.

How about:

function getTotalChildAttr2(parent, child, index) {
    var res = 0;

    angular.forEach(vm.filteredRoutersSummaries[index], function (summary) {
            res += summary[parent][child];
    });

    console.log(vm.filteredRoutersSummaries);

    return res;
}

and then:

vm.routers[i].value = getTotalChildAttr2('real', 'value', i);

Is it ok for you?

Sign up to request clarification or add additional context in comments.

1 Comment

My Super Hero ! Thanks for taking the time to understanding my explication, yeah i know was not very clear, not english native :p, did not really understand how to explain, Thanks again !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.