Lodash _.forInRight() method
What you’ll learn
- How
_.forInRight(object, iteratee)walks own and inherited enumerable string keys from last to first. - The iteratee signature
(value, key, object)and the early-exit pattern (returnfalse). - When reverse iteration genuinely matters (cleanup, last-wins overrides) versus when it’s a smell that you should use an array instead.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.forIn() first; _.forInRight only changes the scan direction.
- Own vs inherited keys: inherited enumerable keys are included—the same surface as
_.forIn. - Iteration order: reverse of
_.forIn’s order. Symbol keys and non-enumerable properties are skipped. - Mutation discipline: deleting keys mid-iteration is safer in reverse order, since it doesn’t shift positions that haven’t been visited yet.
Overview
_.forInRight invokes the iteratee for each own and inherited enumerable string key—but from the last key to the first. The original object is returned, so this is a side-effect helper for logging, cleanup, or last-wins overrides.
Reverse scan
Same key coverage as _.forIn, walked from last to first.
Own + inherited
Enumerable prototype keys are visited too. Use _.forOwnRight if you only care about own keys.
Early exit
Return false from the iteratee to stop before every key is visited.
Syntax
_.forInRight(object, [iteratee=_.identity]) - object: object whose own and inherited enumerable string keys are scanned in reverse.
- iteratee: function invoked with
(value, key, object). - Early exit: return
falseto stop iteration. - Returns: the original object.
Iterate in reverse key order
For a plain object, _.forInRight visits the keys from the last-inserted one back to the first.
import forInRight from "lodash/forInRight";
const user = {
name: "John",
age: 30,
city: "New York"
};
const lines = [];
forInRight(user, (value, key) => {
lines.push(`${key}: ${value}`);
});
// lines -> ["city: New York", "age: 30", "name: John"] Inherited enumerable keys are included
Just like _.forIn, the prototype’s enumerable keys show up. The difference from _.forOwnRight is exactly this inherited coverage.
import forInRight from "lodash/forInRight";
import forOwnRight from "lodash/forOwnRight";
function Animal(name) {
this.name = name;
}
Animal.prototype.sound = "Unknown";
const cat = new Animal("Whiskers");
const forInRightKeys = [];
forInRight(cat, (value, key) => forInRightKeys.push(key));
const forOwnRightKeys = [];
forOwnRight(cat, (value, key) => forOwnRightKeys.push(key));
// forInRightKeys -> ["sound", "name"]
// forOwnRightKeys -> ["name"] Reverse cleanup with early exit
Reverse iteration is a good fit when later resources depend on earlier ones—the most recently allocated thing is released first. Returning false stops the loop the moment a problem is detected.
import forInRight from "lodash/forInRight";
const resources = {
database: { close: () => "db closed" },
cache: { close: () => "cache closed" },
logger: null
};
const log = [];
forInRight(resources, (resource, key) => {
if (!resource) {
log.push(`skip ${key}`);
return false; // stop here
}
log.push(resource.close());
});
// log -> ["skip logger"]
// the loop bailed out before touching cache or database
// because logger was scanned first in reverse order. 📋 _.forInRight vs _.forIn vs _.forOwnRight
| Topic | _.forInRight | _.forIn | _.forOwnRight |
|---|---|---|---|
| Own keys | Yes | Yes | Yes |
| Inherited enumerable keys | Yes | Yes | No |
| Iteration order | Reverse | Forward | Reverse |
| Symbol keys | No | No | No |
| Callback args | (value, key, object) | (value, key, object) | (value, key, object) |
| Early exit | Return false | Return false | Return false |
Pick _.forInRight when both inherited keys and reverse direction matter. If only direction matters and you only care about own keys, _.forOwnRight is the safer default.
Pitfalls to avoid
“Reverse” refers to key order, not chronological time
If timestamps or insertion semantics matter, use an array of entries and reverse that explicitly instead of leaning on object key order.
Prototype keys may surprise you
Inherited enumerable keys are included. Use _.forOwnRight if you only want fields stored directly on the object.
It returns the original object
Returning a transformed value from the iteratee does not build an output object. Use _.mapValues or _.transform for transformations.
Adding keys mid-iteration is undefined territory
Deleting keys during a reverse walk is generally safe, but inserting new keys can shift positions or cause skipped/double visits. Plan structural changes outside the loop.
❓ FAQ
Summary
- Purpose: iterate own and inherited enumerable string keys from last to first.
- Remember: iteratee receives
(value, key, object); returnfalseto break; result is the original object. - Next: Lodash _.forOwn(), _.forIn(), or the official Lodash docs for _.forInRight.
_.forInRight reverses only the iteration order—not the shape of the data. The object itself is not mutated unless your iteratee writes back into it through the third argument.
6 people found this page helpful
