5
votes

I was trying to use the "includes" method for an array in Google Apps Script but it fails with "Cannot find function includes in object 1,4,3,7. (line 4, file "test_array"). Here is the code:

    function test_array() {
    var array1 = [1,4,3,7];
    Logger.log(Array.isArray(array1)); // returns true
    var proof = array1.includes("A"); 
     // proof fails with "Cannot find function includes in object 1,4,3,7. 
     // (line 4, file "test_array")
  Logger.log(proof);
}

In the logs I see that the Logger.log() returns true. I worked around this with:

function test_array() {
  var array1 = [1,4,3,7];
  Logger.log(Array.isArray(array1)); // returns true
  var proof = array1.indexOf("A"); // Works fine
  Logger.log(proof);
}

But I still want to know why the includes method fails on a variable the compiler says is an array. Could it be that it is considering it to be an array of arrays, i.e. an object?

Thanks,

1
what does includes return, if it fails? - Nina Scholz
Array.prototype.includes is fairly recent, the version of js is probably older. - Jared Smith
Unfortunately, this cannot be used at GAS in the current stage. Because includes() has been added to ECMAScript 2015. - Tanaike
Hi Nina, it returns true if element is found or false if it is not. - Ricardo Coto Oviedo
I'm facing the same problem. Use indexOf instead. - Peter Zhao

1 Answers

0
votes

It was/is not supported in runtime. With upgrade to , Array.includes is supported and should be preferred instead of Array.indexOf in all cases.