Javascript 谷歌应用程序脚本中的每个循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46693496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
For-each loop in google apps script
提问by phlaxyr
I'm writing a straightforward email bot handler thing using Google Apps Script.
Say there's an array of something.
I want to iterate through the array using a for-each loop.
(It's tedious, writing for(var i=0;i<threads.length;i++)everytime I iterate through an array.)
I'm looking for a for-each loop for google apps script.
I've already seen thisanswer, but the object is undefined, presumably because the for loop doesn't work.
我正在使用 Google Apps 脚本编写一个简单的电子邮件机器人处理程序。
说有一个数组。
我想使用 for-each 循环遍历数组。
(这很乏味,for(var i=0;i<threads.length;i++)每次我遍历一个数组时都要写。)
我正在为 google 应用程序脚本寻找一个 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.
}
}
(I'm still new to javascript, but I know of a for-each loop from java.)
(我还是 javascript 的新手,但我知道 java 中有一个 for-each 循环。)
回答by Branden Huggins
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', ...).
Example:
例子:
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);
}
}
Result:
结果:
[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
回答by B Bau
In the new V8 runtime Google has removed the for eachloop. (V8 migration)
在新的 V8 运行时中,Google 删除了for each循环。(V8 迁移)
// V8 runtime
var obj = {a: 1, b: 2, c: 3};
for (var key in obj) { // OK in V8
var value = obj[key];
Logger.log("value = %s", value);
}
Old Syntaxdeprecated
旧语法已弃用
// Rhino runtime
var obj = {a: 1, b: 2, c: 3};
// Don't use 'for each' in V8
for each (var value in obj) {
Logger.log("value = %s", value);
}
回答by Jason Fry
From MDN, The for...in statement iterates over the enumerable properties of an object, in original insertion order. For each distinct property, statements can be executed.So you don't want a for...instatement. You could use forEach(), which executes a provided function once for each array element, though you don't have a function in your question so maybe that's not what you want. map()is another option, but it also needs a function, The map() method creates a new array with the results of calling a provided function on every element in the calling array.
来自 MDN,The for...in statement iterates over the enumerable properties of an object, in original insertion order. For each distinct property, statements can be executed.所以你不需要for...in声明。您可以使用forEach(),它为每个数组元素执行一次提供的函数,尽管您的问题中没有函数,所以这可能不是您想要的。map()是另一种选择,但它也需要一个函数, map() 方法创建一个新数组,其结果是在调用数组中的每个元素上调用提供的函数。

