Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JavaScript Array findIndex() Method with Example

In this tutorial, we will learn about the JavaScript Array findIndex() method with the help of example programs.

The findIndex() method works similarly to the find() method, but it returns the index of the first array element that meets the certain criteria defined in the callback function.

In other words, find() method of array returns the index of first array element that satisfies provided testing function (i.e. callback function). Otherwise, it returns -1 if no matching is found.

For example, the following snippet code returns the index of the first number that’s greater than 2.

Program code 1:


Output:
      Index of first greater number than 2: 2

JavaScript Array findIndex() Syntax


The general syntax of the findIndex() method in JavaScript is:

// Arrow function
   arr.findIndex((element) => { /* ... */ } )
   arr.findIndex((element, index) => { /* ... */ } )
   arr.findIndex((element, index, array) => { /* ... */ } )

// Callback function
   arr.findIndex(callbackFn)
   arr.findIndex(callbackFn, thisArg)

// Inline callback function
   arr.findIndex(function(element) { /* ... */ })
   arr.findIndex(function(element, index) { /* ... */ })
   arr.findIndex(function(element, index, array){ /* ... */ })
   arr.findIndex(function(element, index, array) { /* ... */ }, thisArg)

Here, arr is the name of an array.

findIndex() Parameters


The Array.findIndex() method can take three parameters that are:

  • callbackFn – The callback function to execute on each array element; The callbackFn needs to return true for satisfying element in the array. If so, the findIndex() method immediately returns the index of that element. Else, findIndex() returns -1. The function can take the following arguments:
    • element – The current element to be passed in the array.
    • index – The index of the current element being passed in the array.
    • array – The array on which the findIndex() method is called.
  • thisArg – Object to use as this inside the callback function. It is optional.

findIndex() Return Value


This method returns the index of the first element in the array that satisfies the provided callback function. Otherwise, returns -1 if no elements satisfy the function.

The findIndex() method executes the callback function once for each element in the array until it finds a truthy value.

If there is more than one element in the array that will return a truthy value, then findIndex returns only the index of the first element in the array.

Array findIndex() Method Example Programs


Let’s create a JavaScript program in which we will create an array of numbers and find the index of first odd number in the array using findIndex() method of array.

Program code 2:


Output:
      Index of first odd number: 0

Let us create the preceding program using arrow function.

Program code 3:


Output:
      Index of first odd number: 0

Let us create a JavaScript program to find the index of first programming language that begins with the letter “J”.

Program code 4:


Output:
       3

Thus, we can find easily the index of first matching element based on the specified criteria using findIndex() method of array.

Find index of an object in an array by one of its properties


Let’s create a JavaScript program to find the index of an object in an array by one of its properties using findIndex() method of array.

Program code 5:


Output:
      1

Program code 6: Using arrow function


Output:
       2

Return -1 if no matching element found


Program code 7:


Output: 
      -1

In this tutorial, you have learned array findIndex() method in JavaScript with the help of example programs. Hope that you will have understood how to find the index of the first matching element in an array based on specific test condition using findIndex() method.
Thanks for reading!!!

The post JavaScript Array findIndex() Method with Example appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

JavaScript Array findIndex() Method with Example

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×