Syntax, Parameter and Return Value
Syntax:
_.forOwnRight(object, [iteratee=_.identity])

You can also try this code with Online Javascript Compiler
Run Code
Parameters:
-
object (Object): The object to iterate over.
- [iteratee=_.identity] (Function): The function invoked per iteration.
Return Value:
Returns the passed-in object.
Examples
Reverse Iteration Over Object Properties:
JavaScript
var _ = require('lodash');
var user = { 'name': 'John', 'age': 30, 'active': true };
_.forOwnRight(user, function(value, key) {
console.log(key, value);
});

You can also try this code with Online Javascript Compiler
Run Code
// Possible Output: 'active' true, 'age' 30, 'name' John
Demonstrates iterating over an object's own properties in reverse order.
Modifying Properties During Reverse Iteration:
JavaScript
var data = { 'a': 1, 'b': 2, 'c': 3 };
_.forOwnRight(data, function(value, key) {
data[key] = value * 3;
});
console.log(data);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'a': 3, 'b': 6, 'c': 9 }

You can also try this code with Online Javascript Compiler
Run Code
Shows how to modify each property's value during reverse iteration.
Collecting Keys in Reverse Order:
JavaScript
var keys = [];
_.forOwnRight(user, function(value, key) {
keys.push(key);
});
console.log(keys);

You can also try this code with Online Javascript Compiler
Run Code
Possible Output:
['active', 'age', 'name']

You can also try this code with Online Javascript Compiler
Run Code
An example of collecting an object's keys in reverse order.
Breaking Out of the Iteration:
var object = { 'a': 1, 'b': 2, 'c': 3 };
_.forOwnRight(object, function(value, key) {
if (value === 2) {
return false; // Break out of the iteration
}
});

You can also try this code with Online Javascript Compiler
Run Code
Demonstrates stopping the reverse iteration based on a condition.
Frequently Asked Questions
How does _.forOwnRight() differ from _.forOwn()?
_.forOwn() iterates over an object's properties in the order they were added, while _.forOwnRight() does so in reverse.
Can _.forOwnRight() be used with arrays?
While _.forOwnRight() can technically be used with arrays, it is primarily designed for objects. For arrays, methods like _.forEachRight() are more appropriate.
Is it safe to modify the object being iterated over?
Modifying the object during iteration can lead to unexpected behavior, so it's generally advisable to avoid directly modifying the object's structure (like adding or removing properties) during iteration.
Conclusion
Lodash's _.forOwnRight() method provides a useful way to iterate over an object's own properties in reverse order. It's particularly valuable in scenarios where the order of property processing is significant, such as handling recent changes or applying overrides in a specific sequence.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.