Lodash _.find() method
What you’ll learn
- How
_.find(collection, predicate, fromIndex)returns the first match—orundefined. - When optional
fromIndexhelps resume scans without slicing arrays manually. - How this differs from
_.filter(all matches) and when to prefer nativeArray.prototype.find. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Review _.filter() if you want every matching row—find targets only the first hit.
- You understand optional chaining or guards for possibly undefined results.
- You can open Try-it labs or run snippets locally.
Overview
_.find answers “give me one representative row that qualifies”—authorization lookups, config picking, or locating the first failing validator input without scanning the tail unless necessary.
First match wins
Traversal stops immediately—ideal when duplicates exist but any valid exemplar suffices.
Indexed shortcuts
Arrays honor fromIndex so you can paginate or resume scans cheaply.
Unified iteratees
Property paths and matcher objects behave just like on filter or map.
Syntax
_.find(collection, predicate, fromIndex) - collection: array, array-like, or plain object Lodash can iterate.
- predicate: invoked as
(value, index|key, collection); first truthy predicate result yields thatvalue. - fromIndex: optional start offset for array-like collections (defaults to
0). - Returns: the first matching element/value, or
undefinedif none qualify.
Locate a record by partial match
Matcher objects behave like shorthand predicates—perfect for primary-key style lookups in cached lists.
import find from "lodash/find";
find(
[
{ id: "u1", tier: "free" },
{ id: "u2", tier: "pro" },
{ id: "u3", tier: "team" }
],
{ id: "u2" }
);
// → { id: "u2", tier: "pro" } Resume scanning with fromIndex
Skip leading elements when you already ruled them out—no intermediate slice required.
import find from "lodash/find";
find([2, 4, 6, 9, 10], (n) => n % 2 === 1, 3);
// → 9 (odd after index 3) First matching value in an object
Objects iterate values in Lodash order—you receive the actual value, not its key, when something matches.
import find from "lodash/find";
find(
{ east: { score: 12 }, west: { score: 44 }, south: { score: 9 } },
(region) => region.score >= 40
);
// → { score: 44 } 📋 _.find vs filter, findLast, native find
| API | Outcome | Best when |
|---|---|---|
_.find(collection, predicate) | First match or undefined | You only need one qualifying element |
_.filter(collection, predicate) | Array of all matches | You need every passing row |
_.findLast(collection, predicate) | Last match or undefined | Trailing occurrences matter most |
array.find(predicate) | First match or undefined | Arrays only—no object collections |
Pitfalls to avoid
undefined is ambiguous
Both “missing row” and “field literally undefined” need different guards—structure APIs accordingly.
Object iteration order
Which match counts as “first” follows Lodash enumeration—not always insertion order for exotic keys.
Arbitrary winner
When duplicates exist and stability matters, sort upstream or switch to filter.
❓ FAQ
Summary
- Purpose:
_.find(collection, predicate, fromIndex)returns the first matching value. - Contrast: choose
_.filterwhen you must retain every hit. - Next: Lodash _.findLast(), Lodash _.filter() (previous), or collection hub.
_.find stops at the first truthy predicate hit—unlike _.filter, which always walks the whole collection to gather every match. Use find when you only care whether a representative row exists.
6 people found this page helpful
