Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: #<Object> is not a function

Tags:

javascript

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:

enter image description here

console.log(this.selectedApp) logs the object:

enter image description here

As you can see the object in the array and the object in this.selectedApp is the same. So the function should output 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.)

like image 971
alexchenco Avatar asked Apr 10 '16 05:04

alexchenco


People also ask

What is uncaught TypeError?

According to the Mozilla website for developer documents, “the TypeError object represents an error when a value is not of the expected type.” Uncaught means that the error was not caught in the catch part of the try-catch block.

How do I fix uncaught TypeError is not a function?

The TypeError: "x" is not a function can be fixed using the following suggestions: Paying attention to detail in code and minimizing typos. Importing the correct and relevant script libraries used in code. Making sure the called property of an object is actually a function.

How do you handle uncaught TypeError?

Another way to handle TypeError is to use the alert function. The alert function will display a message to the user and then stop the execution of the JavaScript code. In the following code example, the alert function is used to display a message when the y variable is not a number.

What is a TypeError?

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type. A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or.


2 Answers

Your problem is due the fact that findIndex expect a function as a parameter not an object.

Inside of this function you define the condition to find the first the index of the first match.

For example if you are looking for the index of the number 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.

Inside of the function that is passed to findIndex you have 3 parameters that can be used for test.

  • element, is the current element being processed present in the original array.
  • index, The index of the current element being processed, or the result if the test evaluates to true.
  • array The original array findIndex was called upon.

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

like image 123
Crisoforo Gaspar Avatar answered Oct 04 '22 01:10

Crisoforo Gaspar


If you want to get the index of a value in an array, use indexOf:

return this.activeApps.indexOf(this.selectedApp)
like image 38
Felix Kling Avatar answered Oct 04 '22 01:10

Felix Kling