【问题标题】:Why can't I get an array length after I split a string in google script?为什么我在谷歌脚本中拆分字符串后无法获得数组长度?
【发布时间】:2020-01-03 18:18:37
【问题描述】:

在 Google 脚本中,拆分字符串后,我无法获得结果数组的长度。我猜这不是一个数组?如果是这样,我怎样才能把它变成一个有长度值的数组?

function splitAndCount() {
  str = "33,34,35"; //example string
  var stringSplit = str.split(","); //split string
  return stringSplit.length(); //
  // TypeError: Cannot call property length in object 33,34,35. It is not a function, it is "number". 
}

【问题讨论】:

  • length 不是方法,而是数字属性。没有括号。该错误确实说明了一切。它是一个数字,而不是一个函数。所以stringSplit.length.

标签: javascript google-apps-script variable-length


【解决方案1】:

不是函数,是属性

function splitAndCount() {
  str = "33,34,35"; //example string
  var stringSplit = str.split(","); //split string
  return stringSplit.length
}

console.log(splitAndCount())

【讨论】:

    【解决方案2】:

    从stringSplit.length中去掉括号,它不是函数

    function splitAndCount() {
      str = "33,34,35"; //example string
      var stringSplit = str.split(","); //split string
      return **stringSplit.length**; 
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      相关资源
      最近更新 更多