[英]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. 在传递给findIndex
的function
内部,您有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
。 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.