admin管理员组

文章数量:1323323

I'm trying to display the top 5 videos of a channel. However I'm struggling to limit it to just the five.

var playListURL = ';alt=json&callback=?';
var videoURL= '=';
$.getJSON(playListURL, function(data) {
var list_data="";

    $.each(data.feed.entry, function(i, item) {
            var feedTitle = item.title.$t;
            var feedURL = item.link[1].href;
            var fragments = feedURL.split("/");
            var videoID = fragments[fragments.length - 2];
            var url = videoURL + videoID;
            var thumb = "/"+ videoID +"/default.jpg";
            list_data += '<a href="'+ url +'" title="'+ feedTitle +'"><div class="thumbcrop" style="background-image:url('+thumb+');background-size:cover; background-position:center;"></div></a>';
    });
$(list_data).appendTo(".videoClass");
});

How can I achieve this? I have tried (unsuccessfully) clauses such as while(i<6) and if(i<6) but neither of them display any results at all.

I'm trying to display the top 5 videos of a channel. However I'm struggling to limit it to just the five.

var playListURL = 'http://gdata.youtube./feeds/api/users/example/uploads?v=2&alt=json&callback=?';
var videoURL= 'http://www.youtube./watch?v=';
$.getJSON(playListURL, function(data) {
var list_data="";

    $.each(data.feed.entry, function(i, item) {
            var feedTitle = item.title.$t;
            var feedURL = item.link[1].href;
            var fragments = feedURL.split("/");
            var videoID = fragments[fragments.length - 2];
            var url = videoURL + videoID;
            var thumb = "http://img.youtube./vi/"+ videoID +"/default.jpg";
            list_data += '<a href="'+ url +'" title="'+ feedTitle +'"><div class="thumbcrop" style="background-image:url('+thumb+');background-size:cover; background-position:center;"></div></a>';
    });
$(list_data).appendTo(".videoClass");
});

How can I achieve this? I have tried (unsuccessfully) clauses such as while(i<6) and if(i<6) but neither of them display any results at all.

Share asked Jun 26, 2013 at 10:47 BenBen 9,0119 gold badges47 silver badges82 bronze badges 1
  • Is there a reason why you are not using max-results as a parameter in your playListURL? Is there something I am not catching? – Ilu Commented Jun 26, 2013 at 10:56
Add a ment  | 

2 Answers 2

Reset to default 4

In the url that you are requesting your results you can add an extra parameters &max-results=5 so you can restrict the number of your feeds.

so you will have

var playListURL = 'http://.../uploads?v=2&alt=json&max-results=5&callback=?';

More on youtube data api parameters

If you don't want to get all the results, don't use each, which does iterate through all the results in the first place. Simple as that. For limited and known number of steps use simple logic as:

for (var i = 0; i <= 4; i++) {
    var item = data.feed.entry[i];
    // your other stuff goes here
}

...or, as other state correctly, ommit your query by 5 in the first place and then use each freely.

本文标签: javascriptLimit number of JSON resultsStack Overflow