Syntax, Parameter, and Return Value
Syntax:
_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])
Parameters:
-
array (Array): The array to inspect.
-
predicate (Function): The function invoked per iteration.
- fromIndex (optional, number): The index to search from, default is the array's length minus one.
Return Value:
The index of the last element predicate returns truthy for, else -1.
Examples
Basic Usage:
Finding the last index of a positive number.
JavaScript
let numbers = [-1, 0, 1, 2, 3, -2, -3];
let index = _.findLastIndex(numbers, (n) => n > 0);
console.log(index);
You can also try this code with Online Javascript Compiler
Run Code
Output:
4
Object Array:
Finding the last index where an object property matches a value.
JavaScript
let users = [
{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
let index = _.findLastIndex(users, { 'user': 'pebbles', 'active': true });
console.log(index);
You can also try this code with Online Javascript Compiler
Run Code
Output:
2
Custom Predicate Function:
Using a custom function to find the last index of an element.
JavaScript
let numbers = [1, 2, 3, 4, 5];
let index = _.findLastIndex(numbers, (n) => n % 2 === 0);
console.log(index);
You can also try this code with Online Javascript Compiler
Run Code
Output:
3
Specifying Start Index:
Starting the search from a specific index in reverse.
JavaScript
let numbers = [1, 2, 3, 4, 5, 3];
let index = _.findLastIndex(numbers, (n) => n === 3, 4);
console.log(index);
You can also try this code with Online Javascript Compiler
Run Code
Output:
2
Frequently Asked Questions
How does _.findLastIndex() differ from _.findIndex()?
The _.findLastIndex() searches from the end of the array, while _.findIndex() searches from the beginning.
Can this method handle nested array structures?
Yes, with an appropriately defined predicate function, it can navigate and evaluate nested arrays or objects.
Is _.findLastIndex() efficient for large arrays?
Yes, it is efficient for large arrays, particularly because it starts from the end, reducing iteration time for reverse searching.
Conclusion
The _.findLastIndex() method in Lodash is a key tool for reverse searching in arrays. It is especially beneficial in scenarios where the latest or the last occurrences are of interest. This method's ability to seamlessly integrate into JavaScript code, offering simplicity and efficiency, makes it a preferred choice for developers dealing with complex arrays and data structures.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.