5

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,

8
  • what does includes return, if it fails? Commented May 10, 2018 at 21:12
  • 3
    Array.prototype.includes is fairly recent, the version of js is probably older. Commented May 10, 2018 at 21:12
  • 7
    Unfortunately, this cannot be used at GAS in the current stage. Because includes() has been added to ECMAScript 2015. Commented May 10, 2018 at 22:18
  • Hi Nina, it returns true if element is found or false if it is not. Commented May 11, 2018 at 20:41
  • 1
    I'm facing the same problem. Use indexOf instead. Commented Jun 27, 2018 at 7:57

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.