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.

2

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

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.