Lodash _.findLastKey() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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

javascript
_.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 undefined if no key matches.
1

Find the last key whose value passes a test

The object has two values greater than 25, but _.findLastKey scans backward and returns "d".

javascript
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
Try it Yourself
2

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.

javascript
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"
Try it Yourself
3

Matcher shorthand and dotted-path gotcha

Object matcher keys are literal. For nested paths, use a nested matcher object or the [path, value] shorthand.

javascript
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)
Try it Yourself

📋 _.findLastKey vs _.findKey vs _.find

Topic_.findLastKey_.findKey_.find
ReturnsMatching key (string)Matching key (string)Matching value
Works onObjectsObjectsArrays & objects
Iteration orderReverseForwardForward
No-match resultundefinedundefinedundefined
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

Order

“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.

Arg 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.

Matcher

Dotted keys in object matcher are literal

Use { details: { active: true } } or ["details.active", true], not { "details.active": true }.

No-match

Guard undefined

When no value matches, the result is undefined. Check it before using it to read object[key].

❓ FAQ

undefined. Always guard the result before indexing back into the object.
They use the same predicate rules and return a key, but _.findKey scans forward while _.findLastKey scans in reverse order and returns the last matching key.
(value, key, object). Value comes first, then the key, then the original object. This is the same signature style used by _.findKey.
Anything Lodash iteratee supports: a function, a string property name, an array [path, value], or an object used as a partial-deep matcher.
No. A matcher object key like 'details.isAdmin' is treated as a literal property name. Use a nested matcher object or the [path, value] shorthand when you need path matching.
Own enumerable string-keyed properties only. Inherited and symbol keys are skipped.

Summary

Did you know?

_.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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful