Iterators
Stay organized with collections
Save and categorize content based on your preferences.
Iterators are a common programming pattern used for traversing a list of objects when
- The size of the list may not be known from the start.
- Loading the entire list into memory at once may prove overly resource
intensive.
Iterators expose two methods: boolean hasNext()
and Object next()
.
Google Ads scripts use the Iterator pattern for fetching Google Ads entities.
Functionally, iterators are not too different from regular arrays, and can make
your code more concise. Compare the code that traverses an array:
for (var i = 0; i < myArray.length; i++) {
let myObject = myArray[i];
}
with code that traverses an iterator:
while (myIterator.hasNext()) {
let myObject = myIterator.next();
}
The following code demonstrates the usage of an iterator over all campaigns in
your account:
var campaignIterator = AdsApp.campaigns().get();
while (campaignIterator.hasNext()) {
let campaign = campaignIterator.next();
console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +
`budget=${campaign.getBudget().getAmount()}`);
}
You can also use built-in JavaScript iteration:
for (const campaign of AdsApp.campaigns()) {
console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +
`budget=${campaign.getBudget().getAmount()}`);
}
Application of withLimit()
to a selector does not change the value of
totalNumEntities()
. x
and y
in the following snippet will have the same
value:
var x = AdsApp.keywords().get().totalNumEntities();
var y = AdsApp.keywords().withLimit(5).get().totalNumEntities();
In order to obtain an Iterator of Google Ads entities, you must construct a
Selector first.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-01-28 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-01-28 UTC."],[[["Iterators in Google Ads scripts are used to efficiently process lists of objects, especially when dealing with large or unknown-sized datasets, by fetching entities one at a time."],["They offer two primary methods, `hasNext()` to check for more items and `next()` to retrieve the next item, similar to how arrays are traversed but without loading the entire list into memory."],["The Google Ads scripts utilize the Iterator pattern for accessing and manipulating various Google Ads entities like campaigns, allowing for streamlined processing and resource management."],["While applying `withLimit()` to a selector constrains the number of fetched entities, it doesn't affect the overall count obtained via `totalNumEntities()`."],["To retrieve an Iterator of Google Ads objects, you first need to define a Selector that specifies the desired entities and their properties."]]],[]]