2016-05-09 5 views
0

このシステムでは、SoundCloudユーザーIDの配列を取り込み、一連のSC.get関数を使用して各ユーザーの情報(ユーザーID、ユーザー名、続く、ジャンルの好み)。SoundCloud API - システム内のIDに基づいてユーザープロファイルを作成する

<script src="//connect.soundcloud.com/sdk.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
<body> 

<script> 
    //Initialize soundcloud API with client ID 
SC.initialize({ 
    client_id: "54cb0ff94ff9313ef6ca4674e9fe3026" 
}); 


var userIds = [9110252, 55829847, 145189041, 4184647, 225458844, 22557004, 109447512]; 

var userids = []; 
var usernames = []; 
var userFollowingsE = []; 
var profile =[]; 
var ready = 0 

for(u = 0; u < userIds.length; u++){ 

    var id = userIds[u]; 
    getUserData(id); 

    function getUserData(userid){ 

    //**************************** USER ***************************// 
    SC.get("https://stackoverflow.com/users/"+id, function(user){ 

    userids.push(user.id); 
    usernames.push(user.username); 
    }); 

    //********************** USER/FOLLOWINGS **********************// 
    SC.get('/users/'+id+'/followings',function(followings) { 

    var userFollowings = []; 

     for(i = 0; i < followings.collection.length; i++) { 
     userFollowings.push(followings.collection[i].username);           
    } 

    userFollowingsE.push(userFollowings); 

     ready++ 
     if(ready >= userIds.length) onComplete(); 
    }); 
    } 

} 

function onComplete(){ 

    console.log(usernames); 
    console.log(userIds); 
    console.log(userFollowingsE); 
} 

var users = [ 
    { userid: this.userids, 
    username: this.usernames, 
    genre: this.genres, 
    followings: this.followings, 
    } 
] 

</script> 
</body> 

システムで実行する必要があるのは、これらの情報ビットをオブジェクト内の対応するユーザーと関連付けることです。

var users = { 
    user9110252: { 
    userid = userid, 
    username = username, 
    genrePref = genre 
    followings = followings 
    } 
}//etc... 

しかし、システムの変更命令からのたびに出力され、私は相互に関連しているとは思わない配列。

私はこれを行う方法がわからないので、ご提案を歓迎します。

答えて

1

結果の配列を記述したようにするには、複数の配列を作成してマージするのではなく、ユーザーオブジェクトを作成する必要があります。

ここで私が試したことは、それを実行し、期待される結果が得られるかどうかを確認することです。また、提供されたデータでそれらを見つけることができなかったので、私はジャンルを含めなかったことに注意してください。

//Initialize soundcloud API with client ID 
 
SC.initialize({ client_id: "54cb0ff94ff9313ef6ca4674e9fe3026" }); 
 

 
var userIds = [9110252, 55829847, 145189041, 4184647, 225458844, 22557004, 109447512], 
 
    users = []; 
 

 
for(var i=0; i<userIds.length; i++){ getUserData(userIds[i]); } 
 

 
function getUserData(userid){ 
 
    // The user Object you'll be building 
 
    var myUser = {}; 
 
    // Grab the info for this user 
 
    SC.get("https://stackoverflow.com/users/"+userid, function(user){ 
 
     myUser.userid = user.id; 
 
     myUser.username = user.username; 
 
     // Then get its followings 
 
     SC.get('/users/'+userid+'/followings',function(followings) { 
 
      myUser.followings = followings.collection.map(function(user){ 
 
       return user.username; 
 
      }); 
 
      // Push that user to the user list 
 
      users.push(myUser); 
 
      // Execute the callback if all users have been fetched 
 
      if(users.length == userIds.length){ onComplete(); } 
 
     }); 
 
    }); 
 
} 
 

 
function onComplete(){ 
 
    console.log(users); 
 
    // Just for the demo: 
 
    document.body.innerHTML = '<pre>' + JSON.stringify(users,0,2) + '</pre>'; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://connect.soundcloud.com/sdk.js"></script>

+0

あなたは命の恩人です!どうもありがとう! – mellows

関連する問題