What are iterators?

Iterators enumerate items that a selector returns. Iterators are similar to arrays except you can't use an index to directly access an item. Iterators also help reduce memory pressure by loading only a single item at a time rather than the entire set of items. Iterators include the following methods.

  • boolean hasNext() — Returns true if the current position is not the last item in the list
  • Object next() — Advances the current position and returns the object at the new position
  • totalNumEntities() — Returns the number of items available in the iterator.

The following code shows how to use an iterator to iterate over all ad groups in your account.

var iterator = AdsApp.adGroups().get();

while (iterator.hasNext()) {
  var adGroup = iterator.next();
}

Note

The iterators don't support the for-of loop construct. For example:

    for (var campaign of AdsApp.campaigns().get())

The following is the list of iterators.

Next steps