Lodash _.findLast() method
What you’ll learn
- How
_.findLast(collection, predicate, fromIndex)selects the trailing match instead of the leading one. - Why audit logs, revision histories, and stacked events often want “last winner” semantics.
- How this differs from
_.findand from array-only_.findLastIndex. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.find() first—the iteratee contract is identical; only traversal direction changes.
- You can reason about reverse-order scans on arrays.
- You can open Try-it labs or run snippets locally.
Overview
_.findLast prefers the newest duplicate, the final flagged row, or the tail-most anomaly—without reversing arrays manually or tracking indices yourself.
Right-to-left arrays
Arrays inspect from the last index backward until something passes—cheap stop once found.
Latest semantics
Model timelines where the final qualifying entry defines current state.
Same iteratees
Reuse matcher objects and path shorthands from map, filter, or find.
Syntax
_.findLast(collection, predicate, fromIndex) - collection: array, array-like, or plain object Lodash can iterate.
- predicate: invoked as
(value, index|key, collection); truthy keeps the candidate in play while scanning backward. - fromIndex: optional bound for array-like collections where the reverse scan begins.
- Returns: the last matching element/value, or
undefinedif none qualify.
Last passing number in an array
Several values may satisfy the rule—findLast keeps the right-most hit.
import findLast from "lodash/findLast";
findLast([5, 8, 12, 15, 18], (n) => n % 2 === 0);
// → 18 Last record matching a partial object
Duplicate tiers or statuses appear often—pick the final occurrence without reversing manually.
import findLast from "lodash/findLast";
findLast(
[
{ id: 1, tier: "pro", seats: 2 },
{ id: 2, tier: "team", seats: 10 },
{ id: 3, tier: "pro", seats: 5 }
],
{ tier: "pro" }
);
// → { id: 3, tier: "pro", seats: 5 } Last qualifying value in an object collection
Plain-object traversal still yields a single winning value—the last one Lodash encounters while scanning backward through its sequence.
import findLast from "lodash/findLast";
findLast(
{ a: { v: 1 }, b: { v: 30 }, c: { v: 7 }, d: { v: 25 } },
(item) => item.v > 10
);
// → { v: 25 } 📋 _.findLast vs find, findLastIndex, native findLast
| API | Outcome | Best when |
|---|---|---|
_.findLast(collection, predicate) | Last matching value | Objects or arrays—Lodash iteratees |
_.find(collection, predicate) | First matching value | Earliest occurrence suffices |
_.findLastIndex(array, predicate) | Last matching index | You need the position, not the element |
array.findLast(predicate) | Last matching value | Modern JS arrays without Lodash shorthands |
Pitfalls to avoid
“Last” depends on enumeration
Do not assume insertion order for all key shapes—verify Lodash iteration matches your mental model.
Mixing up element vs index APIs
Grab findLastIndex when downstream code slices by numeric offset.
Unstable duplicates
If duplicates are unordered noise, sort or normalize before relying on tail picks.
❓ FAQ
Summary
- Purpose:
_.findLast(collection, predicate, fromIndex)returns the last element matching the predicate. - Contrast: switch to
_.findfor head-first scans or_.findLastIndexfor trailing indices. - Next: Lodash _.flatMap(), Lodash _.find() (previous), or collection hub.
Need the index of the last match in an array instead of the element? Use _.findLastIndex() in the array chapter—it pairs mentally with _.findLast the same way findIndex pairs with find.
6 people found this page helpful
