显示特定用户ID的某些标签的30张Instagram图像

我有一个Instagram API调用请求用CrookedSpaces标记的图像,当这些图像返回时我正在过滤数据,确保只有来自某个用户的图像(使用他们的用户ID),代码如下:

$(function() { $.ajax({ type: "GET", dataType: "jsonp", cache: false, url: "https://api.instagram.com/v1/tags/crookedspaces/media/recent/?count=100&access_token=TOKEN", success: function(data) { for (var i = 0; i < 31; i++) { var igUID = data.data[i].user.id; if(igUID === "USER ID") { $(".instagram").append("\ 
\ " + data.data[i].user.id + " " + igUID + "\
\
\ SMALL TEST!\
\
\
\ "); } else { console.log("Else portion of code ran " + elseCount + " time(s)."); ++elseCount; } } } }); });

但是,我只能显示27个图像,因为该特定用户ID没有发布4个图像。 有没有办法强制for循环不递增? 或者从i中减去1而不将代码发送到无限循环中?

这是JSFiddle – http://jsfiddle.net/UQcZP/

你可以使用while循环:

 var i = 0; var used=0; while (used<31) { if (i < data.data.length){ // .. Do your work here // only increment used when you actually get a match } else { // Here we are past the end of the data and did not reach the 30 items, // so just set the number of items to 31 to exit the loop. used = 31; } i++; }​