Cannot Read Property ‘FindIndex’ Of Undefined In JavaScript – How To Solve It?

“Cannot Read Property ‘FindIndex’ Of Undefined" In JavaScript – How To Solve It?

Are you having trouble dealing with the error “cannot read property ‘findIndex’ of undefined” in JavaScript and are confused about how to fix it? This article will help you solve it.

Why does the error “cannot read property ‘findIndex’ of undefined” in JavaScript happen?

Let’s see the following example to better understand this method:

 let array = [9, 14, 32, 6, 85, 50];
 let isLargeNumber = (Element) => Element > 40;
 console.log(array.findIndex( isLargeNumber ));

Output:

4

Here is an example of how the error occurs:

let array ;
let isLargeNumber = (Element) => Element > 40;
console.log(array.findIndex( isLargeNumber ));

Output:

The error “cannot read property ‘findIndex’ of undefined” in JavaScript that’s because the findIndex() method is called to an undefined value. 

How to fix this error?

Method 1: Check the method call to the specified array

The array.findIndex() method allows you to execute a callback function that iterates over each element in the array and returns the index of the first found element in the given array that satisfies a given condition. Otherwise, it returns -1, indicating that no elements pass the condition.

Syntax:

arr.findIndex(function( currentValue, index, array), thisValue)

Parameters:

  • function: A function to be run for each array element (required).
  • currentValue: the name of a variable used to assign the value of the element being taken from the array (required).
  • index: the name of a variable used to assign the index of the element being retrieved from the array (optional).
  • arr: the name of a variable used to assign the original array (optional).
  • thisValue: A value passed to the function as this value (optional, default: undefined).

Ensure the method is called to the defined array; otherwise, add values ​​to that array. Like the first example, the array must have values, not empty.

Method 2: Use the Array.isArray() method 

Before you call the findIndex() method, you should use the Array.isArray() method to check if a value is an array or not.

Syntax:

Array.isArray(object)

Parameters:

  • Object: is the object to be tested.

Example:

let array1;
 
if(Array.isArray( array1)){
    let array2 = array1.findIndex( element => element > 40);
    console.log( array2 );
} else {
    console.log( 'array1 is not an array' );
}

Output:

array1 is not an array

After that you can fix it as follows:

let array1 = [31, 12, 46, 55, 68];
 
if(Array.isArray( array1 )){
    let array2 = array1.findIndex( element => element > 40);
    console.log(array2);
} else {
    console.log( 'array1 is not an array' );
}

Output:

2

Summary

This article has given two ways to help you to solve the problem “cannot read property ‘findIndex’ of undefined” in JavaScript. They are effective for you, let’s try them. If you have any questions or solutions, please leave a comment below, we are ready to answer any questions. Thank you for reading.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *