Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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 Object Properties:
4.2.
JavaScript
4.3.
Modifying Properties During Reverse Iteration:
4.4.
JavaScript
4.5.
Collecting Keys in Reverse Order:
4.6.
JavaScript
4.7.
Breaking Out of the Iteration:
5.
Frequently Asked Questions
5.1.
How does _.forOwnRight() differ from _.forOwn()?
5.2.
Can _.forOwnRight() be used with arrays?
5.3.
Is it safe to modify the object being iterated over?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.forOwnRight() Method

Author Riya Singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In JavaScript, iterating over an object's properties in reverse order can be crucial, especially when the order of processing is significant. Lodash's _.forOwnRight() method caters to this need by iterating over an object's own enumerable properties in reverse, executing a function for each property. 

Lodash _.forOwnRight() Method

This method is particularly useful when you need to process an object's properties starting from the last added and moving backwards, ensuring that recent changes are addressed first.

Why This Function is Used

The _.forOwnRight() function is used to iterate over an object's own properties in reverse order. This is important in scenarios where the sequence in which properties are processed matters, such as when undoing actions, applying overrides, or handling dependencies that are order-sensitive. By iterating in reverse, it ensures that the most recently added or modified properties are considered first.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.forOwnRight(object, [iteratee=_.identity])

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

JavaScript

var _ = require('lodash');

var user = { 'name': 'John', 'age': 30, 'active': true };

_.forOwnRight(user, function(value, key) {

 console.log(key, value);

});

// 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

JavaScript

var data = { 'a': 1, 'b': 2, 'c': 3 };

_.forOwnRight(data, function(value, key) {

 data[key] = value * 3;

});

console.log(data);

Output:

{ 'a': 3, 'b': 6, 'c': 9 }


Shows how to modify each property's value during reverse iteration.

Collecting Keys in Reverse Order:

  • JavaScript

JavaScript

var keys = [];

_.forOwnRight(user, function(value, key) {

 keys.push(key);

});

console.log(keys);

Possible Output: 

['active', 'age', 'name']


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
  }
});


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 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