JavaScript Array findIndex() method


The findIndex() method in JavaScript is used to search through an array and return the index of the first element that satisfies the condition specified in a provided callback function. If no elements satisfy the condition, it returns -1.

Syntax:

let resultIndex = array.findIndex(callback(currentValue, index, array), thisArg)
  • callback: A function that is called for each element in the array. It can accept up to three parameters:

    • currentValue: The current element being processed in the array.
    • index (optional): The index of the current element being processed.
    • array (optional): The array findIndex() was called upon.
  • thisArg (optional): A value to use as this when executing the callback function.

Return Value:

  • The index of the first element that satisfies the provided testing function. Otherwise, it returns -1 if no elements match the condition.

Key Points:

  • Stops searching after finding the first match: Once a matching element is found, findIndex() immediately returns the index of that element and does not continue to search through the rest of the array.
  • Does not modify the original array: The findIndex() method does not alter the original array; it simply returns the found index or -1.
  • Can be used with objects: It is often used with arrays of objects to find the index of a specific object based on a property.

Example 1: Basic usage (Finding the index of a number)

let numbers = [10, 20, 30, 40, 50]; let index = numbers.findIndex(function(number) { return number > 30; // Find the index of the first number greater than 30 }); console.log(index); // 3 (the index of 40)

Example 2: Using an arrow function (Finding the index of an object)

let users = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' } ]; let index = users.findIndex(user => user.id === 2); // Find index of user with id 2 console.log(index); // 1

Example 3: Returning -1 when no match is found

let fruits = ['apple', 'banana', 'orange']; let index = fruits.findIndex(fruit => fruit === 'grape'); // Look for a fruit that does not exist console.log(index); // -1

Example 4: Using index and array parameters

let numbers = [5, 12, 8, 130, 44]; let index = numbers.findIndex(function(element, index) { return element > 10; // Find index of the first element greater than 10 }); console.log(index); // 1 (the index of 12)

Example 5: Using thisArg

let threshold = { min: 10 }; let numbers = [3, 6, 9, 12, 15]; let index = numbers.findIndex(function(num) { return num > this.min; // Find index of the first number greater than threshold.min }, threshold); console.log(index); // 3 (the index of 12)

Summary:

  • The findIndex() method is an efficient way to search for the index of an element in an array based on specific criteria defined in a callback function.
  • It returns the index of the first matching element or -1, making it useful for locating elements within collections of data.
  • The method can be particularly powerful when working with arrays of objects, allowing easy retrieval of an index based on object properties.