The JavaScript Array.findLastIndex() method is used to search an array and return the index of the last element that satisfies a given condition (provided by a callback function). If no element meets the condition, it returns -1. Unlike the findIndex() method, which starts searching from the beginning of the array, the findLastIndex() method searches from the end to the beginning.
For example, array.findLastIndex(callback) is useful when you need to find the index of the last matching element in an array based on certain criteria.
Syntax
The syntax for the Array.findLastIndex() method is:
array.findLastIndex(callback(element, index, array), thisArg)
Parameters
The Array.findLastIndex() method accepts two parameters:
- callback (Required): A function that is executed on each element of the array, starting from the last element. It takes the following arguments:
- element: The current element being processed.
- index (Optional): The index of the current element.
- array (Optional): The array being traversed.
- thisArg (Optional): Value to use as this when executing the callback function. If not provided, undefined is used.
Return Value
The index of the last element that satisfies the condition. If no element matches the condition, it returns -1.
Examples of JavaScript Array.findLastIndex() Method
Example 1: Finding the Index of the Last Number Greater Than a Given Value
You can use the findLastIndex() method to search for the index of the last number in an array that meets a certain condition.
const numbers = [5, 12, 8, 130, 44];
const foundIndex = numbers.findLastIndex(num => num > 10);
console.log(foundIndex); // Output: 4
Explanation
- The findLastIndex() method returns the index of the last element in the numbers array that is greater than 10. In this case, it returns 4, which is the index of 44.
Example 2: Finding the Index of the Last Object in an Array of Objects
You can use the findLastIndex() method to locate the index of the last object in an array based on a specific property.
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
];
const userIndex = users.findLastIndex(user => user.id < 3);
console.log(userIndex);
// Output: 1
Explanation
- The findLastIndex() method searches the users array from the end and returns the index of the last object where the id property is less than 3. In this case, it returns the index 1, which corresponds to { id: 2, name: 'Bob' }.
Example 3: Using findLastIndex() with Index and Array Parameters
You can access both the index and array parameters in the callback function to create more complex searches.
const numbers = [4, 9, 16, 25];
const foundIndex = numbers.findLastIndex((num, index) => num > 10 && index < 3);
console.log(foundIndex); // Output: 2
Explanation
- The findLastIndex() method looks for the last element that is greater than 10 and whose index is less than 3. It returns 2, which is the index of 16, the last element that meets both conditions.
Example 4: Using findLastIndex() with thisArg
You can provide a custom this context inside the callback function by using the thisArg parameter.
const threshold = {
min: 20
};
const numbers = [5, 12, 8, 130, 44];
const foundIndex = numbers.findLastIndex(function(num) {
return num > this.min;
}, threshold);
console.log(foundIndex); // Output: 3
Explanation
- The thisArg parameter is used to set the this value to the threshold object. The findLastIndex() method returns the index of the last element greater than the min value of 20, which is 3, corresponding to the element 130.
Example 5: When No Match Is Found
If no elements satisfy the condition, the findLastIndex() method returns -1.
const numbers = [1, 2, 3, 4, 5];
const foundIndex = numbers.findLastIndex(num => num > 10);
console.log(foundIndex); // Output: -1
Explanation
- Since no elements in the numbers array are greater than 10, the findLastIndex() method returns -1.
Example 6: Using findLastIndex() on an Empty Array
When the findLastIndex() method is called on an empty array, it always returns -1.
const emptyArray = [];
const foundIndex = emptyArray.findLastIndex(num => num > 10);
console.log(foundIndex); // Output: -1
Explanation
- Since the array is empty, there are no elements to test against the condition, and the method returns -1.