迭代器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
迭代器是一种常见的编程模式,用于在以下情况下遍历对象列表
- 一开始可能并不知道列表的大小。
- 一次性将整个列表加载到内存中可能会过于消耗资源。
迭代器公开了两种方法:boolean hasNext()
和 Object next()
。Google Ads 脚本使用迭代器模式来提取 Google Ads 实体。
从功能上讲,迭代器与常规数组没有太大区别,并且可以使您的代码更简洁。对比遍历数组的代码:
for (var i = 0; i < myArray.length; i++) {
let myObject = myArray[i];
}
使用遍历迭代器的代码:
while (myIterator.hasNext()) {
let myObject = myIterator.next();
}
以下代码演示了如何对账号中的所有搜索广告系列和展示广告系列使用迭代器:
var campaignIterator = AdsApp.campaigns().get();
while (campaignIterator.hasNext()) {
let campaign = campaignIterator.next();
console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +
`budget=${campaign.getBudget().getAmount()}`);
}
您还可以使用内置的 JavaScript 迭代:
for (const campaign of AdsApp.campaigns()) {
console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +
`budget=${campaign.getBudget().getAmount()}`);
}
将 withLimit()
应用于选择器不会更改 totalNumEntities()
的值。以下代码段中的 x
和 y
将具有相同的值:
var x = AdsApp.keywords().get().totalNumEntities();
var y = AdsApp.keywords().withLimit(5).get().totalNumEntities();
为了获取 Google Ads 实体的迭代器,您必须先构建一个选择器。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eIterators 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.\u003c/p\u003e\n"],["\u003cp\u003eThey offer two primary methods, \u003ccode\u003ehasNext()\u003c/code\u003e to check for more items and \u003ccode\u003enext()\u003c/code\u003e to retrieve the next item, similar to how arrays are traversed but without loading the entire list into memory.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Ads scripts utilize the Iterator pattern for accessing and manipulating various Google Ads entities like campaigns, allowing for streamlined processing and resource management.\u003c/p\u003e\n"],["\u003cp\u003eWhile applying \u003ccode\u003ewithLimit()\u003c/code\u003e to a selector constrains the number of fetched entities, it doesn't affect the overall count obtained via \u003ccode\u003etotalNumEntities()\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo retrieve an Iterator of Google Ads objects, you first need to define a Selector that specifies the desired entities and their properties.\u003c/p\u003e\n"]]],[],null,["# Iterators are a common programming pattern used for traversing a list of objects\nwhen\n\n- The size of the list may not be known from the start.\n- Loading the entire list into memory at once may prove overly resource intensive.\n\nIterators expose two methods: `boolean hasNext()` and `Object next()`.\nGoogle Ads scripts use the Iterator pattern for fetching Google Ads entities.\n\nFunctionally, iterators are not too different from regular arrays, and can make\nyour code more concise. Compare the code that traverses an array: \n\n for (var i = 0; i \u003c myArray.length; i++) {\n let myObject = myArray[i];\n }\n\nwith code that traverses an iterator: \n\n while (myIterator.hasNext()) {\n let myObject = myIterator.next();\n }\n\nThe following code demonstrates the usage of an iterator over all Search and\nDisplay campaigns in your account: \n\n var campaignIterator = AdsApp.campaigns().get();\n\n while (campaignIterator.hasNext()) {\n let campaign = campaignIterator.next();\n console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +\n `budget=${campaign.getBudget().getAmount()}`);\n }\n\nYou can also use built-in JavaScript iteration: \n\n for (const campaign of AdsApp.campaigns()) {\n console.log(`${campaign.getName()}; active? ${campaign.isEnabled()}; ` +\n `budget=${campaign.getBudget().getAmount()}`);\n }\n\nApplication of `withLimit()` to a selector does not change the value of\n`totalNumEntities()`. `x` and `y` in the following snippet will have the same\nvalue: \n\n var x = AdsApp.keywords().get().totalNumEntities();\n var y = AdsApp.keywords().withLimit(5).get().totalNumEntities();\n\nIn order to obtain an Iterator of Google Ads entities, you must construct a\n[Selector](/google-ads/scripts/docs/concepts/selectors) first."]]