Lodash _.forOwnRight() method
What you’ll learn
- How
_.forOwnRight(object, iteratee)walks an object’s own enumerable string keys from last to first. - The iteratee signature
(value, key, object)and the early-exit pattern (return false). - Why reverse direction is safer when you intend to delete keys mid-loop.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.forOwn() first if the iteratee shape is new; _.forOwnRight only changes the scan direction.
- Own properties: only keys assigned directly to the object are visited—same set as
Object.keys(). - Iteration order: reverse of
_.forOwn. Symbol keys and non-enumerable properties are skipped. - Mutation discipline: deleting the current key while walking in reverse never shifts unvisited positions—great for inline cleanup.
Overview
_.forOwnRight invokes the iteratee for each own enumerable string key—but from the last key to the first. The original object is returned, which makes this a side-effect helper for last-wins overrides, cleanup, or reverse logging.
Reverse scan
Same key coverage as _.forOwn, walked from last to first.
Own keys only
Inherited prototype keys are skipped automatically—no hasOwnProperty boilerplate.
Early exit
Return false from the iteratee to stop before every key is visited.
Syntax
_.forOwnRight(object, [iteratee=_.identity]) - object: object whose own enumerable string keys are iterated in reverse.
- iteratee: function invoked with
(value, key, object). - Early exit: return
falseto stop iteration. - Returns: the original object.
Iterate own keys in reverse order
For a plain object, _.forOwnRight visits each own key once, starting from the last-inserted one.
import forOwnRight from "lodash/forOwnRight";
const sampleObject = { a: 1, b: 2, c: 3 };
const lines = [];
forOwnRight(sampleObject, (value, key) => {
lines.push(`${key}: ${value}`);
});
// lines -> ["c: 3", "b: 2", "a: 1"] Inherited keys are still skipped
The Right suffix only flips iteration order. _.forOwnRight still ignores enumerable prototype keys; _.forInRight visits them.
import forOwnRight from "lodash/forOwnRight";
import forInRight from "lodash/forInRight";
function Animal(name) {
this.name = name;
}
Animal.prototype.sound = "Unknown";
const cat = new Animal("Whiskers");
const ownRightKeys = [];
forOwnRight(cat, (value, key) => ownRightKeys.push(key));
const inRightKeys = [];
forInRight(cat, (value, key) => inRightKeys.push(key));
// ownRightKeys -> ["name"]
// inRightKeys -> ["sound", "name"] In-place cleanup: drop null / undefined
Reverse iteration is a natural fit when you delete the current key—you never disturb a slot you still need to visit.
import forOwnRight from "lodash/forOwnRight";
const userData = {
name: "John",
age: 25,
isAdmin: false,
lastLogin: null
};
forOwnRight(userData, (value, key, obj) => {
if (value === null || value === undefined) {
delete obj[key];
}
});
// userData -> { name: "John", age: 25, isAdmin: false }
// note: isAdmin: false is KEPT (false is a real value, not nullish) 📋 The four-helper family: own/inherited × forward/reverse
| Helper | Inherited keys? | Direction | When to pick |
|---|---|---|---|
_.forOwn | No | Forward | Default loop over a plain data object. |
_.forOwnRight | No | Reverse | Own-only iteration where last-wins or delete-current semantics matter. |
_.forIn | Yes | Forward | Surface mixin / prototype-provided keys too. |
_.forInRight | Yes | Reverse | Mixin + reverse semantics (rare). |
All four share the (value, key, object) signature and the return false early-exit rule, so swapping between them is mechanical.
Pitfalls to avoid
“Reverse” refers to key order, not chronological time
If insertion-time semantics matter, use an array of entries and reverse that explicitly instead of leaning on object key order.
Picked the wrong helper
If you actually need prototype-provided enumerable keys, use _.forInRight. _.forOwnRight will silently skip them.
Returning a new value does nothing
The iteratee’s return value is only checked for false (early exit). Use _.mapValues or build a result object yourself for transformations.
Be careful with falsy guards
if (!value) also deletes 0, "", and false. Use value == null (or an explicit comparison) when you really mean “nullish”.
❓ FAQ
Summary
- Purpose: iterate an object’s own enumerable string keys from last to first.
- Remember: iteratee receives
(value, key, object); returnfalseto break; result is the original object. - Next: Lodash _.functions(), _.forOwn(), or the official Lodash docs for _.forOwnRight.
_.forOwnRight finishes the four-helper family: forIn / forInRight walk own + inherited keys; forOwn / forOwnRight stick to own keys. The Right suffix only reverses the iteration order—it never changes which keys are visited.
6 people found this page helpful
