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 Array Elements:
4.2.
JavaScript
4.3.
Processing Object Properties in Reverse:
4.4.
JavaScript
4.5.
Breaking Out of the Loop:
4.6.
JavaScript
4.7.
Reverse Iteration Over a String:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
What are typical use cases for _.forEachRight()?
5.2.
Can _.forEachRight() modify the original collection?
5.3.
Is there a performance difference between _.forEach() and _.forEachRight()?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.forEachRight() Method

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

Introduction

Traversal of collections in reverse order can be crucial in certain JavaScript scenarios. Lodash provides a handy method, _.forEachRight(), designed for iterating over collections like arrays and objects from the end to the beginning. 

Lodash _.forEachRight() Method

This article aims to explore the _.forEachRight() method, highlighting its syntax, use cases, and functionality through examples and FAQs.

Why This Function is Used

The _.forEachRight() function is particularly useful when the order of processing is important and needs to be reversed. It's commonly used in situations where the latest elements are prioritized, or a reverse order logic is required, such as undo functionalities in applications, reverse searching, or processing data in a stack-like structure. This method offers a straightforward way to iterate in reverse, enhancing flexibility in Data manipulation and iteration patterns.

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: 

_.forEachRight(collection, [iteratee=_.identity])

Parameters:

  • collection (Array|Object): The collection to iterate over.
     
  • [iteratee=_.identity] (Function): The function invoked per iteration.

Return Value: 

Returns the original collection.

Examples 

Reverse Iteration Over Array Elements:

  • JavaScript

JavaScript

var _ = require('lodash');

var numbers = [1, 2, 3];

_.forEachRight(numbers, value => console.log(value * 2));

 Output:

 6, 4, 2


Demonstrates iterating over an array in reverse order.

Processing Object Properties in Reverse:

  • JavaScript

JavaScript

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

var keys = Object.keys(user);

_.forEachRight(keys, key => console.log(key, user[key]));

Output:

 'age' 30, 'name' John


Shows reverse iteration over the properties of an object.

Breaking Out of the Loop:

  • JavaScript

JavaScript

_.forEachRight(numbers, (value) => {

 if (value === 2) return false; // Breaks the loop

 console.log(value);

});

Output:

 3


Ilustrates stopping reverse iteration based on a condition.

Reverse Iteration Over a String:

  • JavaScript

JavaScript

var string = 'hello';

_.forEachRight(string, char => console.log(char.toUpperCase()));

 Output:

 O, L, L, E, H


An example of iterating over each character of a string in reverse order.

Frequently Asked Questions 

What are typical use cases for _.forEachRight()?

Typical use cases include processing data in reverse order, like traversing a stack, reverse searching in arrays, or implementing undo operations.

Can _.forEachRight() modify the original collection?

Similar to _.forEach(), if the iteratee function modifies the elements, those changes will reflect in the original collection.

Is there a performance difference between _.forEach() and _.forEachRight()?

Performance differences are generally negligible; the choice between them is based more on the required order of iteration.

Conclusion

Lodash's _.forEachRight() method is an effective solution for iterating over collections in reverse order. It provides the flexibility to handle scenarios where reverse processing is needed, enhancing the ability to manage data in various structures and formats.

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