| by Arround The Web | No comments

Array.findIndex() in ES6

In JavaScript, there are various methods utilized for different purposes. For instance, the “Array.slice()” is used to define the piece of data of an array in a new array. “Array.pop()” is utilized to eliminate the unnecessary array element and return a new array. However, the “Array.findIndex()” is invoked to return the index of the initial array element that meets the specified condition.

This post will discuss the Array.findIndex() method in ES6.

What is Array.findIndex() in ES6?

Array.findIndex() is a JavaScript method introduced in ES6. It is utilized for returning the index of an initial array element that meets the expectations according to the stated condition. It does not affect the original array. On the other hand, if no element exists in the array, then it returns the value in the negative form. However, this method could not execute the functions for the array element if there is no value.

How to Use the Array.findIndex() Method in ES6?

To use the array.findIndex() in ES6, try out the provided syntax:

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

Here:

  • 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:
const age = [4, 9, 22, 25];

age.findIndex(checkAge);

Next, define “checkAge()” function and add the “return” statement to return the output of the condition:

function checkAge(age){

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:

const Countries = ["United Kingdom", "Singapore", "Canada", "India"]

Next, define a function with the name “getCountry()”. Then, utilize the “return” statement to output the country with the value “Canada”:

function getCountry(Country) {

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:

console.log(Countries.findIndex(getCountry))

That’s all about using the array.findIndex() method in ES6.

Conclusion

The Array.findIndex() is a JavaScript method introduced in ES6. It is utilized for returning the index of an initial array element that evaluates as true, according to the stated condition. You can also invoke this method to find the index value of numeric as well as text data sets in an array. This tutorial has demonstrated the usage of the Array.findIndex() JavaScript method.

Share Button

Source: linuxhint.com

Leave a Reply