Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对于google应用程序脚本中的每个循环_Javascript_Google Apps Script - Fatal编程技术网

Javascript 对于google应用程序脚本中的每个循环

Javascript 对于google应用程序脚本中的每个循环,javascript,google-apps-script,Javascript,Google Apps Script,如何在GoogleApps脚本中为每个循环创建一个循环 我正在用GAS编写一个电子邮件脚本,我想使用for-each循环(而不是常规的for循环)遍历一个数组。 我已经看到了答案,但对象是未定义的,可能是因为for循环不起作用 // threads is a GmailThread[] for (var thread in threads) { var msgs = thread.getMessages(); //msgs is a GmailMessage[] for (var m

如何在GoogleApps脚本中为每个循环创建一个循环

我正在用GAS编写一个电子邮件脚本,我想使用for-each循环(而不是常规的for循环)遍历一个数组。
我已经看到了答案,但对象是未定义的,可能是因为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循环。)

for…in语句按原始插入顺序迭代对象的可枚举属性。对于每个不同的属性,都可以执行语句。
因此您不希望在语句中为…使用
。您可以使用,它为每个数组元素执行一次提供的函数,尽管您的问题中没有函数,所以这可能不是您想要的。是另一个选项,但它也需要一个函数,map()方法创建一个新数组,其结果是对调用数组中的每个元素调用提供的函数

更新:有关将脚本迁移到V8运行时的更新,请参见下面的@BBau-answer

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', ...). 结果:

[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 [17-10-16 23:34:47:724 EDT]使用for循环打印阵列信息。 [17-10-16 23:34:47:725美国东部时间]0 [17-10-16 23:34:47:725美国东部时间]1 [17-10-16 23:34:47:726美国东部时间]2 [17-10-16 23:34:47:726 EDT]打印每个循环使用的阵列信息。 [17-10-16 23:34:47:727美国东部夏令时]苹果 [17-10-16 23:34:47:728美国东部夏令时]橙色 葡萄柚
在新的V8运行时中,Google删除了每个
循环的
。()

// 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);
}
旧语法已弃用


for(var thread of threads){var msgs=thread.getMessages();//msgs是一个GmailMessage[]for(var msg in msgs){…}for…在对象的键上迭代,而不是它的值。JS中不鼓励使用此模式,因为它将包括添加到阵列中的自定义原型。如果您想要一个更优雅的解决方案,我建议您使用
thread.map(handleThread)
或类似的解决方案,在以后的javaScript版本中,forEach方法被添加到数组对象中。GoogleApps脚本使用的JavaScript引擎具有此模式。请看下面我的答案。好的,我站在正确的位置。好的答案。较短的版本
for each(var ColNum在[5,“hh”,7]){Logger.log(ColNum)}
这里不应该使用它,因为数组类方法是可用的,比如
Array#forEach
Array#map
for each的
东西很有趣,因为它不是标准的JavaScript(对吧?)但是
Array#forEach
不适用于我的用例,因为我需要从循环内部返回。我认为这相当于ES6中(…of…)
。我认为这是更好和最新的答案<每个
的code>在中不兼容,我们应该避免使用它。
// 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);
}
// 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);
}