简体   繁体   English

未捕获的TypeError:# <Object> 不是一个功能

[英]Uncaught TypeError: #<Object> is not a function

The following function returns the index of an object: 以下函数返回对象的索引:

selectedAppIndex: function () {
  console.log(this.activeApps)
  console.log(this.selectedApp)
  return this.activeApps.findIndex(this.selectedApp)
}

console.log(this.activeApps) logs the array: console.log(this.activeApps)记录数组:

在此输入图像描述

console.log(this.selectedApp) logs the object: console.log(this.selectedApp)记录对象:

在此输入图像描述

As you can see the object in the array and the object in this.selectedApp is the same. 如您所见,数组中的对象和this.selectedApp的对象是相同的。 So the function should output 0 . 所以函数应该输出0

However I get this error: 但是我收到此错误:

Uncaught TypeError: #<Object> is not a function

(I'm using Chrome 49. And findIndex() is supported from Chrome 45.) (我使用的是Chrome 49. Chrome 45 支持 findIndex()

Your problem is due the fact that findIndex expect a function as a parameter not an object . 您的问题是由于findIndex期望function作为参数而不是object

Inside of this function you define the condition to find the first the index of the first match. 在此function内部,您可以定义条件以查找第一个匹配的第一个index

For example if you are looking for the index of the number 12: 例如,如果您要查找数字12的索引:

 function  search12(el){
      return el === 12;
 }

 [4, 6, 8, 12].findIndex(search12);

The above example will return the number 3, since 12 is located on the index 3. 上面的例子将返回数字3,因为12位于索引3上。

Inside of the function that is passed to findIndex you have 3 parameters that can be used for test. 在传递给findIndexfunction内部,您有3个可用于测试的参数。

  • element , is the current element being processed present in the original array. element ,是原始数组中存在的当前元素。
  • index , The index of the current element being processed, or the result if the test evaluates to true . index ,正在处理的当前元素的索引,或者测试计算结果为true
  • array The original array findIndex was called upon. array调用原始数组findIndex

More info . 更多信息

As you can see on the function above you have to return the case where you have a match, because the function passed as a parameter is being executed every time per each element until is evaluated to true if some element evaluates to true will return that index if non element inside of the array evaluates to true will return -1 正如你可以在功能见上你必须回到这里你有一个匹配的情况下,因为该function被每个每个元素每次执行时,作为参数传递,直到被评估为true ,如果一些元素的计算结果为true将返回指数如果array内部的非元素计算结果为true,则返回-1

如果要获取数组中值的索引,请使用indexOf

return this.activeApps.indexOf(this.selectedApp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM