It’s a pretty common requirement, especially when you’re posting to an API, to split an array of data into manageable chunks, and most often the solution is to make an array of arrays – with each one being up to a maximum size. That’s fine, but we can also make an iterator.
Let’s take a look at how these approaches differ, and take a look into iterators – a very interesting addition to Apps Script as a result of v8.
Make an array of arrays
This is the usual solution.
Using an iterator
This is slightly more complicated but has the advantage of only creating chunks when they are needed. In the first approach we’d have multiple versions of the input array – a chunked and an unchunked. It’s a more satisfying solution all round.
First we need a function that can make an iterator. We use the [Symbol.iterator] well known symbol to create the iterator method and signal that it is iterable.
Now we can loop through the chunks creating them on demand.
Adding start and end
It might be useful to start and end at a particular slice of the inputArray. Again, to avoid making a redundant slice of the input array, we can build an optional start and end into the iterator.
The default is to use the entire input array as before
but we can also select just a section of the input array
Converting back to an array
Since we can make arrays out of iterators, duplicating the output of the first method (array of arrays) is simple
Related

Iterator magic – Splitting an array into chunks
Read More

The nodejs Drive API, service account impersonation and async iterators
Read More
Rate limit handler, helper and iterator: Apps Script use cases
Read More

ES6 Symbols – what on earth is all that about ?
Read More

Getting ‘a pile of files’ from Google Drive with Apps Script
Read More
Supercharging copying files between Drive and Cloud Storage from Apps Script with Cloud Run
Read More

A handier way of accessing Google Drive folders and files from Apps Script
Read More

Blistering fast file streaming between Drive and Cloud Storage using Cloud Run
Read More

Sharing data between Apps Script and Node using Google Drive back end – 5 minute example
Read More

Searching and cataloging Apps Script projects on Github
Read More