U
U
UUmniYY2013-12-08 21:05:53
JavaScript
UUmniYY, 2013-12-08 21:05:53

Vk Api JS problem when exiting function

There is this piece of code:

function GroupMembers1(data){
    GroupCount1=data.response.count;
    var a=0;
    for(var i=0;i<=GroupCount1; i++){
    VK.api("users.get", {user_ids:data.response.users[i], fields:"sex"}, function(data){
        if(data.response[0].sex==1){
          GroupMember1[a]=data.response.users[i]; 
          a++;
          
        };
      });
    };
  };

It has an array of GroupMember1, and after exiting the function, it is cleared, well, all values ​​become undefined. The array itself was set up earlier.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Petrusha Ukropov, 2013-12-08
@artishok

for(var i=0;i<=GroupCount1 - 1; i++){
Why variable a if there is already i in the loop ?

C
CrackedSapphire, 2013-12-09
@CrackedSapphire

I would pass the resulting array as a parameter so that there is no doubt about where and how it is declared

function GroupMembers1(data , myReslut){
        GroupCount1=data.response.count;
        var a=0;
        for(var i=0;i<=GroupCount1; i++){
        VK.api("users.get", {user_ids:data.response.users[i], fields:"sex"}, function(data){
                if(data.response[0].sex==1){
                    myResult[a]=data.response.users[i]; 
                    a++;
                    
                };
            });
        };
    };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question