Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value:
4.
Examples 
4.1.
Reverse Iteration Over Properties:
4.2.
Prioritizing Recent Properties:
4.3.
JavaScript
4.4.
Using _.forInRight() in Custom Functions:
4.5.
JavaScript
4.6.
Breaking Out of the Iteration:
5.
Frequently Asked Questions
5.1.
How does _.forInRight() differ from _.forIn()?
5.2.
Can _.forInRight() be used with arrays?
5.3.
Is modifying the object during iteration safe?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.forInRight() Method

Author Riya Singh
0 upvote

Introduction

Iterating over an object's properties, including those inherited, is a common requirement in JavaScript programming. Lodash extends this functionality with the _.forInRight() method, which iterates over an object's own and inherited properties in reverse order. 

Lodash _.forInRight() Method

This method is particularly useful when the order of iteration is important, such as when you need to prioritize recently added or overridden properties in complex objects with inheritance.

Why This Function is Used

The _.forInRight() function is used for iterating over an object's properties in reverse, starting from the most recently added or overridden properties and moving towards the original properties. It's especially useful in scenarios where the sequence of property processing matters, such as when the latest changes to an object need to be considered first, or when working with objects that have a deep prototype chain where inherited properties are significant.

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass