我收到了Instagram API的回复,结果遇到了麻烦。在检查重复项之后,我只是将每个标记调用响应的数据对象推送到我的空结果数组中。
我的控制台截图如下。
奇怪的是,结果是这样的。在for循环检查重复项时,长度似乎正常工作。
var token = 'MY_TOKEN',
userid = MY_ID,
num_photos = 33,
hashtags = ['website', 'engraving', 'weddinginvitations', 'customstationery'],
results = [],
test_results = [{id:1},{id:2},{id:3}];
// LOOP THROUGH EACH HASH
for(i = 0; i < hashtags.length; i++){
// MAKE AJAX CALL PER HASH
$.ajax({
url: 'https://api.instagram.com/v1/tags/' + hashtags[i] + '/media/recent',
dataType: 'jsonp',
type: 'GET',
data: {access_token: token, count: num_photos},
success: function(response){
// CHECK EACH IMAGE IN THE RESPONSE DATA AND REMOVE DUPLICATES
for( x in response.data ){
var exists = false;
for(var i=0;i<results.length;i++){
if( results[i].id === response.data[x].id ){
exists = true;
break;
}
}
if(exists === false){
results.push(response.data[x]);
}
}
},
error: function(data){
console.log(response);
}
});
}
console.log(results);
console.log(results.length);
console.log(test_results);
console.log(test_results.length);