【问题标题】:Array "includes" method fails in Google Apps ScriptGoogle Apps 脚本中的数组“包含”方法失败
【发布时间】:2018-10-21 05:39:24
【问题描述】:

我试图在 Google Apps 脚本中对数组使用“包含”方法,但它失败并显示“无法在对象 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);
}

在日志中,我看到 Logger.log() 返回 true。我解决了这个问题:

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);
}

但我仍然想知道为什么编译器说是数组的变量上的 include 方法会失败。莫非是把它当成一个数组数组,即一个对象?

谢谢,

【问题讨论】:

  • 如果失败,includes 会返回什么?
  • Array.prototype.includes 比较新,js的版本可能比较旧。
  • 很遗憾,this cannot be used at GAS in the current stage. 因为includes() 已添加到 ECMAScript 2015 中。
  • 嗨 Nina,如果找到元素则返回 true,否则返回 false。
  • 我也面临同样的问题。请改用indexOf

标签: javascript google-apps-script


【解决方案1】:

运行时支持/不支持。 With upgradeArray.includes 受支持,在所有情况下都应优先使用 Array.indexOf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2021-08-02
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多