简体   繁体   English

数组“包含”方法在 Google Apps 脚本中失败

[英]Array “includes” method fails in Google Apps Script

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:我试图在 Google Apps 脚本中对数组使用“includes”方法,但它失败了“无法在对象 1、4、3、7 中找到包含函数。(第 4 行,文件“test_array”)。这是代码:

    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.在日志中,我看到 Logger.log() 返回 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, ie an object?难道它正在考虑它是一个数组数组,即一个对象?

Thanks,谢谢,

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. 随着升级Array.includes得到支持,并且在所有情况下都应该是首选而不是Array.indexOf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM