【问题标题】:indexOf in google script谷歌脚本中的 indexOf
【发布时间】:2017-12-01 03:13:34
【问题描述】:

我正在尝试在 google 脚本中的对象上使用 findIndex(),但它不起作用。这是一个例子:

function myFunction() {
var searchingJohn = "John"; 
var DATA = {namesList: []};  
 DATA.namesList.push({name: "John", otherDataForThisName: []});
 var result = DATA.namesList.findIndex(function (element)
       {return element.name == this;}, searchingJohn);
 return result;
}

这在 javascript 控制台中运行良好,但 google 脚本返回“TypeError:Fonction findIndex not found in object.....”

【问题讨论】:

  • 什么是谷歌脚本?
  • 你用的是什么浏览器? findIndex 是 ES6 方法,在 IE 中不可用。
  • 另外,this 在回调函数中也不起作用。

标签: javascript google-apps-script indexof


【解决方案1】:

你不能。

您尝试在对象上使用findIndex(),但findIndex() 用于数组。

来自 findIndex() 上的 MDN 文档:

findIndex() 方法返回数组 中满足提供的测试函数的第一个元素的索引。否则返回 -1。

原因是对象不以任何特定顺序保存键值对。例如,

{key1:value1, key2:value2}

{key2:value2, key1:value1}

是完全相同的对象。所以找不到索引。

【讨论】:

    【解决方案2】:

    这是使用findIndex() 函数在google 脚本的对象中搜索字符串的示例。示例:

    function myFunction() { 
      var DATA = {namesList: []}; 
      DATA.namesList.push({name: "John", otherDataForThisName: []});
      DATA.namesList.push({name: "Mark", otherDataForThisName: []});
      DATA.namesList.push({name: "Twin", otherDataForThisName: []});
      const searching = "Mark";
     
      var result = DATA.namesList.findIndex(function (element){
         return element.name.toLowerCase() == searching.toLowerCase();
      });
      return result;
    }
    

    findIndex() 函数也用于二维数组。

    【讨论】:

      【解决方案3】:
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var cellRange = ss.getRange("B5:Q10").getValues();
      var someString = "John";
      
      if (cellRange == "John")
      {
          //Do this code.
      }
      

      这是我能想到的提取您想要获取的信息的唯一方法。指数();如果 "John" 在数组中,则从索引中提取它看起来像这样 ["J", "o", "h", "n"] 这就是为什么不能以这种方式找到它的原因。

      上面的代码会在一系列单元格中找到它,您可以执行整个工作表,但添加的“空”越多,运行速度就越慢。如果您需要检查大量工作表,另一个嵌套的 if 循环可以为您清理。

      【讨论】:

        【解决方案4】:

        我在使用 gscript 的许多基本 javascript 功能上遇到了同样的问题,我在脚本顶部添加了 polyfill,它可以与许多功能一起使用。

        将此代码粘贴到脚本顶部,它将支持 indexOf。

        // This version tries to optimize by only checking for "in" when looking for undefined and
        // skipping the definitely fruitless NaN search. Other parts are merely cosmetic conciseness.
        // Whether it is actually faster remains to be seen.
        if (!Array.prototype.indexOf)
          Array.prototype.indexOf = (function(Object, max, min) {
            "use strict"
            return function indexOf(member, fromIndex) {
              if (this === null || this === undefined)
                throw TypeError("Array.prototype.indexOf called on null or undefined")
        
              var that = Object(this), Len = that.length >>> 0, i = min(fromIndex | 0, Len)
              if (i < 0) i = max(0, Len + i)
              else if (i >= Len) return -1
        
              if (member === void 0) {        // undefined
                for (; i !== Len; ++i) if (that[i] === void 0 && i in that) return i
              } else if (member !== member) { // NaN
                return -1 // Since NaN !== NaN, it will never be found. Fast-path it.
              } else                          // all else
                for (; i !== Len; ++i) if (that[i] === member) return i 
        
              return -1 // if the value was not found, then return -1
            }
          })(Object, Math.max, Math.min)
        

        参考https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Polyfill

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-01
          • 2020-04-02
          • 2020-12-20
          • 1970-01-01
          • 1970-01-01
          • 2016-10-08
          • 1970-01-01
          相关资源
          最近更新 更多