2016-05-09 73 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

相關問題