Syntax, Parameter, and Return Value
Syntax:
_.findIndex(array, [predicate=_.identity], [fromIndex=0])
Parameters:
-
array (Array): The array to inspect.
-
predicate (Function): The function invoked per iteration.
- fromIndex (optional, number): The index to search from, default is 0.
Return Value:
The index of the first element predicate returns truthy for, else -1.
Examples
Basic Usage:
Finding the index of the first positive number.
JavaScript
let numbers = [-3, -2, -1, 0, 1, 2];
let index = _.findIndex(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 index of an object where a property matches a value.
JavaScript
let users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
let index = _.findIndex(users, { 'user': 'fred', 'active': false });
console.log(index);
You can also try this code with Online Javascript Compiler
Run Code
Output:
1
Using a Custom Predicate Function:
Using a custom function to find the index of an element.
JavaScript
let numbers = [1, 2, 3, 4, 5];
let index = _.findIndex(numbers, (n) => n % 2 === 0);
console.log(index);
You can also try this code with Online Javascript Compiler
Run Code
Output:
1
Specifying Start Index:
Starting the search from a specific index.
JavaScript
let numbers = [1, 2, 3, 4, 5];
let index = _.findIndex(numbers, (n) => n > 3, 2);
console.log(index);
You can also try this code with Online Javascript Compiler
Run Code
Output:
3
Frequently Asked Questions
What happens if no element satisfies the predicate?
If no element satisfies the predicate, _.findIndex() returns -1, indicating no match was found.
Can _.findIndex() handle nested objects or arrays?
Yes, it can handle nested structures, provided the predicate function is designed to navigate and evaluate them appropriately.
How is _.findIndex() different from Array.prototype.findIndex()?
While both perform similar tasks, _.findIndex() offers more flexibility with predicates and consistent behavior across different JavaScript environments.
Conclusion
Lodash's _.findIndex() method is a robust and convenient tool for locating the index of the first element in an array that satisfies a specific condition. It simplifies the task of searching through arrays, particularly those with complex structures or large data sets. With its flexible predicate functions and easy-to-understand syntax, _.findIndex() enhances the efficiency of JavaScript coding, especially in scenarios involving data searching, manipulation, and UI-related operations.
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.