javascript - 谷歌脚本中的indexOf

标签 javascript google-apps-script indexof

我尝试在 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......”

最佳答案

你不能。

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

来自关于 findIndex() 的 MDN 文档:

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

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

{key1:value1, key2:value2}

{key2:value2, key1:value1}

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

关于javascript - 谷歌脚本中的indexOf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47586105/

相关文章:

java - 为什么 Vector.indexOf 方法不适用于此类?

jQuery indexof 从第二个相同的字符开始

javascript - ASK - jquery datepicker - 在文本输入中保留选定的日期

javascript - 麻烦造型扩展 polymer 元素(纸 slider )

javascript - 在flipclock.js倒计时器停止后修改页面文本

sms - Twilio 通过 Google Apps 脚本发送和接收短信

javascript - jquery regex .replace 不写入 dom

java - 如何在 Google-App 上允许快速发布请求?

javascript - 转换 2D 数组理解 |米xn尺寸

r - 在R中的排序向量中查找值索引