Syntax, Parameter and Return Value
Syntax:
_.forInRight(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 Properties:
var _ = require('lodash');
function Shape() {
this.x = 0;
this.y = 0;
}
Shape.prototype.move = function(x, y) {
this.x += x;
this.y += y;
};
var circle = new Shape();
circle.radius = 5;
_.forInRight(circle, function(value, key) {
console.log(key, value);
});
You can also try this code with Online Javascript Compiler
Run Code
// Output might include 'move', 'radius', 'y', 'x' in reverse order
Demonstrates iterating over both own and inherited properties of an object in reverse.
Prioritizing Recent Properties:
JavaScript
var data = { 'a': 1, 'b': 2 };
Object.prototype.c = 3;
_.forInRight(data, function(value, key) {
console.log(key, value);
});
You can also try this code with Online Javascript Compiler
Run Code
Output:
'b' 2, 'a' 1, 'c' 3 (Prototype property 'c' is iterated last)
You can also try this code with Online Javascript Compiler
Run Code
Shows prioritizing the object's own properties over inherited properties.
Using _.forInRight() in Custom Functions:
JavaScript
var object = { 'a': 1, 'b': 2, 'c': 3 };
var keys = [];
_.forInRight(object, function(value, key) {
keys.push(key);
});
console.log(keys);
You can also try this code with Online Javascript Compiler
Run Code
Output:
['c', 'b', 'a']
You can also try this code with Online Javascript Compiler
Run Code
An example of collecting keys in reverse order of their addition to the object.
Breaking Out of the Iteration:
_.forInRight(object, function(value, key) {
if (key === 'b') {
return false; // Break out of the iteration
}
});
You can also try this code with Online Javascript Compiler
Run Code
Demonstrates how to stop the iteration based on a condition.
Frequently Asked Questions
How does _.forInRight() differ from _.forIn()?
_.forIn() iterates over an object's properties in the order they were added, while _.forInRight() does so in reverse.
Can _.forInRight() be used with arrays?
While _.forInRight() can technically be used with arrays, it is not the most suitable method for array iteration due to its inclusion of inherited properties. Array-specific methods are usually more appropriate for arrays.
Is modifying the object during iteration safe?
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 _.forInRight() method offers a unique approach to iterating over an object's properties in reverse order, including inherited ones. It's particularly useful for scenarios where the order of property processing is significant, such as prioritizing recent changes or handling complex inheritance structures.
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.