This post will discuss the Array.findIndex() method in ES6.
What is Array.findIndex() in ES6?
How to Use the Array.findIndex() Method in ES6?
To use the array.findIndex() in ES6, try out the provided syntax:
- “currentValue” determines the value that will be found in the array.
- “index” is utilized for determining the array index.
- “arr” is used to define the array.
- “thisValue” is an optional value gone to the function to be used as the function’s “this” value.
Example 1: Utilize the Array.findIndex() With Numeric Values
To utilize the array.findIndex() with numeric values, try out the enlisted instructions:
- Declare and initialize the constant with a particular name and assign the value in numeric form in an array.
- Next, invoke the “findIndex()” method that will find the index according to the specified parameter:
Next, define “checkAge()” function and add the “return” statement to return the output of the condition:
return age > 22;
The function returned “3” which indicates that the value greater than “22” has been found on the third index of the array:
Example 2: Utilize the Array.findIndex() With Text Values
The “array.findIndex()” method can be also used with text values in an array. To do so, define the constant with a specific name and assign the value in an array:
Next, define a function with the name “getCountry()”. Then, utilize the “return” statement to output the country with the value “Canada”:
return Country === "Canada";
Lastly, run the “console.log()” method to display the output on the console. To do so, invoke the findIndex() method and pass “getCountry” as parameter:
That’s all about using the array.findIndex() method in ES6.