这里我有一些代码,使用SoundCloud API,将运行函数来获取用户喜欢和关注的类型,然后将这些项存储在对象中。
我希望能够通过在开头输入用户ID来自动填充需要用户ID的区域,并自动将其放入以创建数据库。
现在我对编码很新,如果我没有解释得很好,我甚至不知道我想做什么是可能的,但任何建议都会很棒!
以下是我失败的尝试,但它应该让你知道我想要做什么。我认为主要问题发生在SoundCloud API引用的区域,例如: SC.get('/users/userIds[i]/favorites', function(favorites)
var userIds = [9110252, 55829847, 145189041]
for(u = 0; u < userIds.length; u++){
var username = localStorage.getItem('useruserIds[i]username');
var genre = localStorage.getItem('useruserIds[i]genrePref');
var followings = localStorage.getItem('useruserIds[i]followings')
/************************* /USERS/FAVORITES.GENRE ***********************/
// Iterate through user's likes to retrieve list of genres
SC.get('/users/userIds[i]/favorites', function(favorites) {
var userGenrePref = [];
for(var i = 0; i < favorites.length; i++){
userGenrePref.push(favorites[i].genre);
}
var userGenrePrefUp = userGenrePref.map(function(x) {return x.toLowerCase();});
// Find the most common genre from the userGenrePrefUp array
var frequency = {}; // array of frequency.
var max = 0; // holds the max frequency.
for(var v in userGenrePrefUp) {
frequency[userGenrePrefUp[v]]=(frequency[userGenrePrefUp[v]] || 0)+1; // increment frequency.
if(frequency[userGenrePrefUp[v]] > max) { // is this frequency > max so far ?
max = frequency[userGenrePrefUp[v]]; // update max.
result = userGenrePrefUp[v]; // update result.
}
}
var result; // holds the max frequency element.
localStorage.setItem('user'+userIds[i]+'genrePref',result);
});
/******************** /USERS/FOLLOWINGS.USERNAME ********************/
// Run through user's followers
SC.get('/users/userIds[i]/followings',function(followings) {
userFollowings = [];
for(i = 0; i < followings.collection.length; i++) {
var bap = followingscollection[i].username
userFollowings.push(bap);
}
//console.log(userFollowings);
localStorage.setItem('useruserIds[i]followings', JSON.stringify(userFollowings));
});
}
var users = {
user : {username: username, genrePref: genre, followings: JSON.parse(followings)},
}
// Users array
var userProfile = [];
// Populate users array
for(var key in users) {
userProfile.push(users[key]);
}
console.log(userProfile);