Lodash _.findLastKey() method
What you’ll learn
- How
_.findLastKey(object, [predicate])returns the last own string key whose value satisfies the predicate. - Why it is not a date-aware or timestamp-aware helper: “last” means reverse key iteration order.
- The same predicate shorthands as
_.findKey: function, string property,[path, value], and partial-deep matcher object. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.findKey() first if the predicate shorthand behavior is new; findLastKey changes only the scan direction.
- Predicate truthiness: Lodash stops at the first truthy predicate result encountered while scanning in reverse.
- Key order: only own enumerable string keys are searched, using JavaScript’s normal property order in reverse.
- Own vs inherited keys: inherited and symbol keys are skipped.
Overview
_.findLastKey scans an object’s own enumerable string keys in reverse order. For each key it invokes the predicate with (value, key, object), then returns the key for the first truthy result it encounters from the right side of the key list.
Returns the key
Use it when you need the property name, not just the matching value.
Reverse scan
Same predicate behavior as _.findKey, but starts from the last key and moves backward.
Shorthand friendly
Function predicates, string properties, [path, value], and matcher objects all work.
Syntax
_.findLastKey(object, [predicate=_.identity]) - object: object whose own enumerable string keys are scanned in reverse.
- predicate (optional): any Lodash iteratee shorthand; defaults to
_.identity. - Predicate signature:
(value, key, object) => boolean. - Returns: the matching key string, or
undefinedif no key matches.
Find the last key whose value passes a test
The object has two values greater than 25, but _.findLastKey scans backward and returns "d".
import findLastKey from "lodash/findLastKey";
const scores = { a: 10, b: 20, c: 30, d: 40 };
findLastKey(scores, (value) => value > 25);
// -> "d"
findLastKey(scores, (value) => value > 50);
// -> undefined findKey vs findLastKey
Both helpers use the same predicate. The difference is direction: forward returns the first enabled entry, reverse returns the last enabled entry.
import findKey from "lodash/findKey";
import findLastKey from "lodash/findLastKey";
const features = {
alpha: { enabled: true },
beta: { enabled: false },
gamma: { enabled: true }
};
findKey(features, "enabled");
// -> "alpha"
findLastKey(features, "enabled");
// -> "gamma" Matcher shorthand and dotted-path gotcha
Object matcher keys are literal. For nested paths, use a nested matcher object or the [path, value] shorthand.
import findLastKey from "lodash/findLastKey";
const accounts = {
guest: { details: { active: true, role: "guest" } },
admin: { details: { active: true, role: "admin" } },
demo: { details: { active: false, role: "guest" } }
};
findLastKey(accounts, { "details.active": true });
// -> undefined ("details.active" is treated as a literal key)
findLastKey(accounts, { details: { active: true } });
// -> "admin" (nested matcher; reverse scan)
findLastKey(accounts, ["details.role", "guest"]);
// -> "demo" ([path, value] supports dotted paths) 📋 _.findLastKey vs _.findKey vs _.find
| Topic | _.findLastKey | _.findKey | _.find |
|---|---|---|---|
| Returns | Matching key (string) | Matching key (string) | Matching value |
| Works on | Objects | Objects | Arrays & objects |
| Iteration order | Reverse | Forward | Forward |
| No-match result | undefined | undefined | undefined |
| Predicate args | (value, key, object) | (value, key, object) | (value, indexOrKey, collection) |
Use _.findLastKey when later keys should win. Use _.findKey when earlier keys should win.
Pitfalls to avoid
“Last” means reverse key order, not latest time
For timestamp keys, sort dates explicitly if chronological order matters. _.findLastKey only reverses JavaScript property iteration order.
Predicate gets (value, key), not (key, value)
The value is the first argument. Use the second argument when your condition depends on the key name.
Dotted keys in object matcher are literal
Use { details: { active: true } } or ["details.active", true], not { "details.active": true }.
Guard undefined
When no value matches, the result is undefined. Check it before using it to read object[key].
❓ FAQ
Summary
- Purpose: find the last own string key whose value satisfies a predicate.
- Remember: reverse key order, predicate is
(value, key, object), and no match returnsundefined. - Next: Lodash _.forIn(), _.findKey(), or the official Lodash docs for _.findLastKey.
_.findLastKey uses the same predicate rules as _.findKey, but scans the object's own enumerable string keys in reverse order. That means duplicate-looking matches return the later key, not the earlier one.
6 people found this page helpful
