1

How do I put multiple GET's in an array?

I want to start with putting all channelId's in an array like this:

var Channels = [ID1, ID2, ID3];

And then a function with $each... But how to do it? :)

Who can help me please? It will be much appreciated

var ItemArray = [];

var d1 = $.get("https://www.googleapis.com/youtube/v3/search?channelId=UCVQ2Z9dNQ2aJJ10f6SgBH0g&type=video&order=date&maxResults=1&part=snippet&KEY",
        function (data) {
            $.each(data.items, function (i, item) {
idee = item.id.videoId;
tittie = item.snippet.title;
cattit = item.snippet.channelTitle;
datie = item.snippet.publishedAt;
ItemArray.push([datie, cattit, idee, tittie]);
            });
        });

var d2 = $.get("https://www.googleapis.com/youtube/v3/search?channelId=UC2xskkQVFEpLcGFnNSLQY0A&type=video&order=date&maxResults=1&part=snippet&KEY",
        function (data) {
            $.each(data.items, function (i, item) {
idee = item.id.videoId;
tittie = item.snippet.title;
cattit = item.snippet.channelTitle;
datie = item.snippet.publishedAt;
ItemArray.push([datie, cattit, idee, tittie]);
            });
        });

var d3 = $.get("https://www.googleapis.com/youtube/v3/search?channelId=UCGHi_s4RrqUh4hsS4mLbiPg&type=video&order=date&maxResults=1&part=snippet&key=KEY",
        function (data) {
            $.each(data.items, function (i, item) {
idee = item.id.videoId;
tittie = item.snippet.title;
cattit = item.snippet.channelTitle;
datie = item.snippet.publishedAt;
ItemArray.push([datie, cattit, idee, tittie]);
            });
        });

$.when(d1, d2, d3).done(function() {

ItemArray.sort(function(a, b) {
        return a[0] - b[0];
});

for(i=0;i<=ItemArray.length;i++){
$('#mytable').append('<tr><td>'+ItemArray[i][0]+'</td><td><a target="_blank" href="https://www.youtube.com/user/'+ItemArray[i][1]+'">'+ItemArray[i][1]+'</a></td><td><a target="_blank" href="https://www.youtube.com/watch?v='+ItemArray[i][2]+'">'+ItemArray[i][3]+'</a></td></tr>');
}
})
})

2
  • 1
    So what is the problem? You do not what the three d1,d2,d3 functions and just have one method that makes all the calls?? Commented Jan 25, 2016 at 15:56
  • Question should identify if there are problems or errors in code shown and what code is expected to do that it isn't currently doing. Commented Jan 25, 2016 at 16:02

2 Answers 2

1

See example below.
The code is commented an should work as expected.
Ask if any questions.

(function($) {
    var ItemArray = [];

    // List all IDs here
    var ids = ["UCVQ2Z9dNQ2aJJ10f6SgBH0g", "UC2xskkQVFEpLcGFnNSLQY0A", "UCGHi_s4RrqUh4hsS4mLbiPg"];
    var done = [];

    // Go through all ids
    $.each(ids, function(index, value) {
        // Save all Deferred in variable done
        done.push($.get("https://www.googleapis.com/youtube/v3/search?channelId=" + value + "&type=video&order=date&maxResults=1&part=snippet&KEY", fn)); // use function fn for doing all the time the same
    });

    function fn(data) {
        $.each(data.items, function(i, item) {
            idee = item.id.videoId;
            tittie = item.snippet.title;
            cattit = item.snippet.channelTitle;
            datie = item.snippet.publishedAt;
            ItemArray.push([datie, cattit, idee, tittie]);
        });
    }
	
    // to use an array as input for when()
    // you need th use the prototype method apply to convert it
    $.when.apply($, done).done(function() {

        // this part stays the same
        ItemArray.sort(function(a, b) {
            return a[0] - b[0];
        });

        for (i = 0; i <= ItemArray.length; i++) {
            $('#mytable').append('<tr><td>' + ItemArray[i][0] + '</td><td><a target="_blank" href="https://www.youtube.com/user/' + ItemArray[i][1] + '">' + ItemArray[i][1] + '</a></td><td><a target="_blank" href="https://www.youtube.com/watch?v=' + ItemArray[i][2] + '">' + ItemArray[i][3] + '</a></td></tr>');
        }
    });
})(jQuery);

Sign up to request clarification or add additional context in comments.

4 Comments

Uncaught SyntaxError: missing ) after argument list. Please format your code and check the braces.
thanks a lot for ur time, i really appreciate it. let me know if you know how to fix it.. :)
There is an } too much in your code. ` } /* <-- This one*/ }); })(jQuery);`
ITS WORKING!!! Thank you so much sammy youre a great guy. i really appreciate it. can i give you kudos or points here somewhere. im really happy :)
0

So loop over the array and make the ajax calls, pushing the Ajax calls into a new array.

var channels = ["a","b","c"],
    calls = channels.reduce( function (arr, current) {
        var xhr = $.get("http://example.com?q=" + current, function() { /* your code ajax success*/}); 
        arr.push(xhr);
        return arr;
    }, []);

//Take that array and apply it to the when
$.when.apply($, calls).done( function () {
   //Your code
} )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.