0

I am developing an application with AngularJS and I have an array of data that I need to loop through and make multiple ajax requests. Once all of the data has been returned for all of requests I would then like manipulate the returned data.

However, when I log compositeSchedule, I get "[]". How can I make all of the requests, then access the compositeSchedule array to manipulate the data?

    var compositeSchedule = [];
    $.each(activeTeams, function(i){
        var params = {};
        params.teams = activeTeams[i].ID;
        params.org = org.URL;
        params.sessionID = org.user.sessionID;
        $http.get("https://api.domain.com", {params: params}).then(function(result){
            compositeSchedule = compositeSchedule.concat(result.data.Events);
        });
    });
    console.log(compositeSchedule);
Mark Kamyszek
  • 398
  • 2
  • 5
  • 15
  • Put the `console.log(compositeSchedule)` inside the `.get` method right after where `compositeSchedule` is defined. – Xenyal Nov 20 '14 at 19:09
  • I'm all for marking the question as a duplicate, but in this case you've linked to the *wrong* question. This guy is asking about how to fire off *multiple* AJAX requests and still get the data. – Ed_ Nov 20 '14 at 19:11
  • @Xenyal But I want to manipulate compositeSchedule once I have all of the data. If I do this in the get call, this is going to manipulate the returned data each time, for the length of the activeTeams array. – Mark Kamyszek Nov 20 '14 at 19:17
  • Instead of `.concat` on your results, use `.push` to add your new results into your array `compositeSchedule`. So the line would be `compositeSchedule.push(result.data.Events);`, no need to redeclare the variable. – Xenyal Nov 20 '14 at 19:20

0 Answers0