2

I've got a simple Google Apps Script code like this:

function throwsError(){
  var inboxThreads = GmailApp.getInboxThreads();
  var sliceArr = inboxThreads.slice(0, 3);
  Logger.log(sliceArr.length);

  for each (var thread in sliceArr){
    Logger.log(inboxThreads.containsThread(thread));
  }
}

function doesNotThrowError(){
  var inboxThreads = GmailApp.getInboxThreads();
  var sliceArr = inboxThreads.slice(0, 3);
  Logger.log(sliceArr.length);

  for (var i = 0; i < sliceArr.length; i++){
    Logger.log(inboxThreads.containsThread(sliceArr[i]));
  }
}

Array.prototype.containsThread = function(thread){
  Logger.log("Here");
  Logger.log(thread);
  return this.filter(function(t){ return t.getId() == thread.getId(); }).length > 0;
}

As the function names indicate, throwsError() throws an error of TypeError: Cannot find function getId in object function (thread) {...}.. doesNotThrowError() runs perfectly without issue. The only difference between them is that one uses a for loop and the other uses a for each loop.

The log output of throwsError() is as follows:

image

It seems that there are only 3 items in my sliceArr array, but the containsThread is being called 4 times. (Additionally, the 4th time it seems that it is passing in the containsThread function into itself as the parameter). Any ideas what could be causing this?

My inclination is that this is a bug with Google Apps Script, but I wanted to check here in case anyone else had some insight. Note that Google Apps Script works on JS 1.6 with some portions of 1.7 and 1.8

derekantrican
  • 1,891
  • 3
  • 27
  • 57
  • `for each (var thread in easyThreads)` doesn't compile for me. Shouldn't it be `for (var thread in easyThreads)`? Or maybe this is peculiar to Google Apps Script... – Stephen O'Connor May 17 '19 at 16:28
  • @SteveO'Connor I think it's particular to Google Apps Script – derekantrican May 17 '19 at 16:51
  • @SteveO'Connor I just changed my question to have the minimum reproducible steps (basically, without the "easyLabel" requirement) – derekantrican May 17 '19 at 17:04
  • 1
    Possible duplicate of [For-each over an array in JavaScript?](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) – TheMaster May 17 '19 at 21:20
  • @TheMaster Note that in some cases, Google Apps Script does not work the same as typical JavaScript. This is one of those cases. [Here is an example of GAS for each](https://stackoverflow.com/a/46782189/2246411) – derekantrican May 17 '19 at 21:28
  • This isn't one of those cases. You should read the references in the second answer of your linked question(especially the MDN one) and the linked duplicate question regarding how `for-in` works and why it shouldn't be used in arrays or if used, how it should be used. – TheMaster May 17 '19 at 21:37
  • 1
    Don't use `for each in`, period. – tehhowch May 17 '19 at 22:28

1 Answers1

0

Don't use for each

From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for_each...in

The for each...in statement is deprecated as the part of ECMA-357 (E4X) standard. E4X support has been removed. Consider using for...of instead.

Firefox now warns about the usage of for each...in and it no longer works starting with Firefox 57.

Please see Warning: JavaScript 1.6's for-each-in loops are deprecated for migration help.

Rubén
  • 34,714
  • 9
  • 70
  • 166