1

I'm using a loop function to navigate through a tree menu list and get their value (number of "devices" in each group) and I callback the same function for each main group and sub-group.

i.e.

ROOT (0)
   ∟ GROUP A (0)
   ∟ GROUP B (0)
   ∟ GROUP C (0)
   ∟ GROUP D (0)
   ∟ GROUP E (0)
 ∟ GROUP F (0)
     ∟ GROUP F.1 (1)
     ∟ GROUP F.2 (1)
     ∟ GROUP F.3 (1)
     ∟ GROUP F.4 (1)
     ∟ GROUP F.5 (1)
function getDevices(groupIds,devicesNum){       

for ( var i = 0; i <groupIds.length; i++ ) {    
    let Id = String(groupIds[i]);       
    $.ajax({
    type : 'GET',
    url : 'https://url.blablabla.bla',
        dataType :'json',
    success : function(data){

    var devicesNum = 0;
        var groupIds = new Array();         
        for ( var i = 0; i < data.items.length; i++ ) {

        groupIds .push ( data.items[i].groupId.toString() );
        devicesNum +=  parseInt(data.items[i].deviceCount) ;

    }

       getBrands(urlServer,token,brandIds,brandDevices)  
        // callback of the same function
},
error : function(error) {
     console.log(error)
    }
});

total = 0;
total += devicesNum;
console.log ( total );  // this is supposed to be the total...
} };

I know the TOTAL number of values/devices is 5, but the console.log (total) returns these:

NaN

0

0

0

0

0

0

0

5

5

5

5

5

So, how can I get the total amount at the end of the loops? I also tried creating a new empty array and pushing the values in for each loop, but nothing changed...

Please help!

j08691
  • 204,283
  • 31
  • 260
  • 272
Italo
  • 33
  • 2

0 Answers0