Iterator
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
반복자는 다음과 같은 경우에 객체 목록을 탐색하는 데 사용되는 일반적인 프로그래밍 패턴입니다.
- 시작부터 목록의 크기를 알지 못하는 경우
- 전체 목록을 한 번에 메모리에 로드하면 리소스가 과도하게 사용될 수 있습니다.
Iterator는 boolean hasNext()
및 Object next()
라는 두 가지 메서드를 노출합니다.
Google Ads 스크립트는 Google Ads 항목을 가져오는 데 Iterator 패턴을 사용합니다.
기능적으로는 반복자가 일반 배열과 크게 다르지 않으며 코드를 더 간결하게 만들 수 있습니다. 배열 탐색에 사용되는 다음 코드와
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 항목의 Iterator를 가져오려면 먼저 선택자를 구성해야 합니다.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-06-04(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-06-04(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."]]],[]]