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...
但是,從系統輸出的數組每次都會改變順序,我不認爲它們是相互關聯的。
我不確定如何做到這一點,並歡迎任何建議。
你是一個生命的救星!非常感謝! – mellows