TypeError:找不到函数使用Array.prototype时 [英] TypeError: Cannot find function When using Array.prototype

查看:94
本文介绍了TypeError:找不到函数使用Array.prototype时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的函数,我试图添加到数组中。我在这个网站上看到过一些例子,人们已经扩展了不同的简单项目,比如String&阵列。但是我也看到了谷歌不允许你扩展某些类的评论。但从我所知道的情况来看,这只适用于自定义的Google类,如电子表格,范围等。



为什么抛出TypeError?试图使用相同的功能我在这里找到

  Array.prototype.findIndex = function(search){
if(search ==)return false;
for(var i = 0; i< this.length; i ++)
if(this [i] == search)return i;

返回-1;
}

//在特定范围内搜索,找到正确的ID并根据ID
函数onEdit(e){
var e = {};
e.value =Test String;
var client = e.value;
var columnValues = mydoc.getRangeByName(categoryRangeName).getValues();

// columnValues返回二维数组值
//https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()
var searchResult = columnValues.findIndex(e.value); // **错误在这里**

}

更新

我还会补充一点,我设置方法返回一个像这样的值只是为了测试原型没有问题,
$ b

  Array.prototype.findIndex = function(val){
return val;

$ / code>

我用相同的for循环创建了一个函数,它工作得很好。

 函数findIndex(vals,search){
if(search ==)return false;
for(var i = 0; i< vals.length; i ++)
if(vals [i] == search)return i;

返回-1;


解决方案 findIndex 函数,在一维数组上搜索,但是,您有一个二维数组。您必须更改 findIndex 函数以在2D数组上进行搜索。

  Array.prototype.findIndex = function(search){
if(search ==='')return false;
for(var i = 0,len = this.length; i< len; i ++)
if(this [i] === search)return i;
返回-1;
}

function onEdit(e){
var search_text ='Test String';
var columnValues1D = ['String Test','Test String'];
var columnValues2D = [['String Test'],['Test String']];
Logger.log(columnValues1D.findIndex(search_text)); //< - 1
Logger.log(columnValues2D [0] .findIndex(search_text)); //< - -1
Logger.log(columnValues2D [1] .findIndex(search_text)); //< - 0
}

更新

  Array.prototype.findIndex = function(search){
if(search == ='')return false;
for(var i = 0,len = this.length; i< len; i ++)
if(this [i] [0] === search)return i;
返回-1;
}

function onEdit(e){
var search_text ='Test String';
var columnValues2D = [['String Test'],['Test String']];
Logger.log(columnValues2D.findIndex(search_text)); //< - 1
}


I have a very simple function I am trying to add to an array. I have seen examples on this site where people have extended different simple items, like String & Array. But I also have seen comments where Google doesn't allow you to extend certain classes. But from what I can tell, that was only for custom Google classes, like Spreadsheet, Range, etc.

Why is this throwing a TypeError? Tried to use the same function I found here.

Array.prototype.findIndex = function(search){
  if(search == "") return false;
  for (var i=0; i<this.length; i++)
    if (this[i] == search) return i;

  return -1;
} 

//Do a search on a specific range, find the correct ID, and do a lookup based on ID
function onEdit(e) {
  var e = {};
  e.value = "Test String";
  var client = e.value;
  var columnValues = mydoc.getRangeByName(categoryRangeName).getValues();

  //columnValues returns 2D array of values
  //https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()
  var searchResult = columnValues.findIndex(e.value);  //**ERROR HERE**

}

UPDATE

I will also add, that I set the method to just return a value like this just to test that there was no problem with the prototype, and it failed on the same error.

Array.prototype.findIndex = function(val) {
  return val;
} 

And I created a function with the same for loop, and it works just fine.

function findIndex(vals, search) {
  if(search == "") return false;
  for (var i=0; i<vals.length; i++)
    if (vals[i] == search) return i;

  return -1;
}

解决方案

The findIndex function, search on a 1D array, however, you have a 2D array. You must change the findIndex function to search on a 2D array.

Array.prototype.findIndex = function(search) {
  if(search === '') return false;
  for (var i = 0, len = this.length; i < len; i++)
    if (this[i] === search) return i;
  return -1;
}

function onEdit(e) {
  var search_text = 'Test String';
  var columnValues1D = ['String Test', 'Test String'];
  var columnValues2D = [['String Test'], ['Test String']];
  Logger.log(columnValues1D.findIndex(search_text)); // <-- 1
  Logger.log(columnValues2D[0].findIndex(search_text)); // <-- -1
  Logger.log(columnValues2D[1].findIndex(search_text)); // <-- 0
}

UPDATE

Array.prototype.findIndex = function(search) {
  if(search === '') return false;
  for (var i = 0, len = this.length; i < len; i++)
    if (this[i][0] === search) return i;
  return -1;
}

function onEdit(e) {
  var search_text = 'Test String';
  var columnValues2D = [['String Test'], ['Test String']];
  Logger.log(columnValues2D.findIndex(search_text)); // <-- 1
}

这篇关于TypeError:找不到函数使用Array.prototype时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆