1

I'm fetching a URL. The full response is spread over five pages.

I'm looping through each pages which returns me an array of object (please correct me if I'm wrong):

[{item_1=foo, item_2=bar, item_3=foobar, value_1=XX}, {item_1=bar, item_2=foo, item_3=barfoo, value_1=XX},etc...]

I want to consolidate all the response like if it was one big array of objects.

So far, I wrote this:

   for (i = 1; i <= total_pages; i++) {

    var rawResponse = UrlFetchApp.fetch(
    'url',
    {
      method: 'GET'
    })

   response[i] = JSON.parse(rawResponse);

    }

  var g = response[1].concat(response[2], response[3],response[4],response[5]);

g contains the desired output; however, as you can see, this is not dynamic. How can I solve this? I could you the push method, but I would return me a new array with each response.

Simon Breton
  • 2,638
  • 7
  • 50
  • 105
  • Does this answer your question? [List more than 30 students in a class in Google Classroom API query](https://stackoverflow.com/questions/49338104/list-more-than-30-students-in-a-class-in-google-classroom-api-query) – tehhowch Jan 07 '20 at 19:00
  • I provide an example pattern to do this in the linked answer https://stackoverflow.com/a/49338510/9337071 – tehhowch Jan 07 '20 at 19:00

1 Answers1

1

In order to make your code "dynamic" you could use the concat function inside the for-loop, for each of the pages. A possible modification of your code could look like the following, where the result variable would contain all the results:

var result = [];

for (var i = 1; i <= total_pages; i++) {
  var rawResponse = UrlFetchApp.fetch(
    'url',
    {
      method: 'GET'
    }
  );

  var current = JSON.parse(rawResponse);
  result = result.concat(current);
}
carlesgg97
  • 4,184
  • 1
  • 8
  • 24