Syntax, Parameter and Return Value
Syntax:
_.find(collection, [predicate=_.identity], [fromIndex=0])
Parameters:
-
collection (Array|Object): The collection to inspect.
-
[predicate=_.identity] (Function): The function invoked per iteration.
- [fromIndex=0] (number): The index to start the search at.
Return Value:
Returns the matched element, else undefined.
Examples
Finding a Specific Number:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5];
var foundNumber = _.find(numbers, n => n > 3);
console.log(foundNumber);

You can also try this code with Online Javascript Compiler
Run Code
Output:
4
This demonstrates finding the first number in an array greater than 3.
Searching for an Object with a Specific Property:
JavaScript
var users = [{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }];
var foundUser = _.find(users, { 'age': 36, 'active': true });
console.log(foundUser);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'user': 'barney', 'age': 36, 'active': true }
Here, _.find() locates an object with specified property values.
Finding an Element in an Object:
JavaScript
var object = { 'a': 1, 'b': 2, 'c': 3 };
var foundElement = _.find(object, v => v > 1);
console.log(foundElement);

You can also try this code with Online Javascript Compiler
Run Code
Output:
2
Shows how _.find() can be used to find a value in an object.
Using a Property Name Shorthand:
JavaScript
var products = [{ 'name': 'apple', 'inStock': true },
{ 'name': 'banana', 'inStock': false }];
var inStockProduct = _.find(products, 'inStock');
console.log(inStockProduct);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'name': 'apple', 'inStock': true }
Utilizing Lodash's property name shorthand for simpler syntax.
Frequently Asked Questions
How does _.find() differ from _.filter()?
While _.filter() returns all elements that meet a condition, _.find() only returns the first element that matches the predicate.
Can _.find() work with nested objects?
Yes, _.find() can navigate nested objects and arrays if the predicate function is designed to handle such structures.
Is _.find() efficient for large collections?
_.find() is efficient for most cases, but its performance depends on the collection size and complexity of the predicate function.
Conclusion
Lodash's _.find() method is an efficient and concise tool for locating the first element in a collection that satisfies a given condition. It streamlines the process of searching through data, enhancing code readability and performance. This method is a testament to Lodash's ability to simplify common data manipulation tasks in JavaScript.
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.