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