我正在使用Google Apps脚本编写简单的电子邮件机器人处理程序.
说有一系列的东西.
我想使用for-each循环遍历数组.
(这很乏味,for(var i=0;i
我正在寻找谷歌应用程序脚本的for-each循环.
我已经看到了这个答案,但是对象是未定义的,大概是因为for循环不起作用.
// threads is a GmailThread[] for (var thread in threads) { var msgs = thread.getMessages(); //msgs is a GmailMessage[] for (var msg in msgs) { msg.somemethod(); //somemethod is undefined, because msg is undefined. } }
(我还是javascript的新手,但我知道来自java的for-each循环.)
In Google Apps Script: When using "for (var item in itemArray)", 'item' will be the indices of itemArray throughout the loop (0, 1, 2, 3, ...). When using "for each (var item in itemArray)", 'item' will be the values of itemArray throughout the loop ('item0', 'item1', 'item2', 'item3', ...).
例:
function myFunction() { var arrayInfo = []; arrayInfo.push('apple'); arrayInfo.push('orange'); arrayInfo.push('grapefruit'); Logger.log('Printing array info using for loop.'); for (var index in arrayInfo) { Logger.log(index); } Logger.log('Printing array info using for each loop.'); for each (var info in arrayInfo) { Logger.log(info); } }
结果:
[17-10-16 23:34:47:724 EDT] Printing array info using for loop. [17-10-16 23:34:47:725 EDT] 0 [17-10-16 23:34:47:725 EDT] 1 [17-10-16 23:34:47:726 EDT] 2 [17-10-16 23:34:47:726 EDT] Printing array info using for each loop. [17-10-16 23:34:47:727 EDT] apple [17-10-16 23:34:47:728 EDT] orange [17-10-16 23:34:47:728 EDT] grapefruit