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.
Summing an Array of Numbers in Reverse:
4.2.
JavaScript
4.3.
Concatenating String Elements in Reverse Order:
4.4.
JavaScript
4.5.
Complex Reduction with Nested Structures:
4.6.
JavaScript
4.7.
Building a Nested Structure in Reverse:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.reduceRight() differ from _.reduce()?
5.2.
Can _.reduceRight() be used with non-array collections?
5.3.
Is _.reduceRight() efficient for large collections?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.reduceRight() Method

Author Rinki Deka
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

In JavaScript programming, sometimes there's a need to aggregate or accumulate values from a collection in reverse order. For such scenarios, Lodash provides the _.reduceRight() method. Similar to _.reduce(), this function iteratively applies a function across the elements of a collection from right to left (or end to start), accumulating the results into a single value. 

Lodash _.reduceRight() Method

This article will focus on the _.reduceRight() method, examining its syntax, usage, and practical applications, illustrated through examples and FAQs.

Why This Function is Used

The _.reduceRight() function is used when a reduction operation needs to be performed in reverse order. This is particularly useful in situations where the order of processing matters, such as when dealing with operations on nested structures, or when the rightmost elements have priority in computation. It offers a way to iterate over a collection backwards, building up a result based on each element.

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: 

_.reduceRight(collection, [iteratee=_.identity], [accumulator])

Parameters:

  • collection (Array|Object): The collection to iterate over in reverse.
     
  • [iteratee=_.identity] (Function): The function invoked per iteration.
     
  • [accumulator]: The initial value to start accumulation.

Return Value:

 Returns the accumulated value.

Examples 

Summing an Array of Numbers in Reverse:

  • JavaScript

JavaScript

var _ = require('lodash');

var numbers = [1, 2, 3, 4, 5];

var sum = _.reduceRight(numbers, (sum, n) => sum + n, 0);

console.log(sum);

 Output: 

15


Demonstrates adding up the numbers in an array, starting from the end.

Concatenating String Elements in Reverse Order:

  • JavaScript

JavaScript

var strings = ['hello', 'world', 'lodash'];

var concatenated = _.reduceRight(strings, (result, str) => result + ' ' + str, '');

console.log(concatenated);

Output:

'lodash world hello'


Shows concatenation of strings in an array, starting from the last element.

Complex Reduction with Nested Structures:

  • JavaScript

JavaScript

var collections = [{ 'count': 5 }, { 'count': 10 }, { 'count': 15 }];

var totalCount = _.reduceRight(collections, (sum, obj) => sum + obj.count, 0);

console.log(totalCount);

Output: 

30


Demonstrates summing values from a collection of objects in reverse order.

Building a Nested Structure in Reverse:

  • JavaScript

JavaScript

var nestedArrays = [[1, 2], [3, 4], [5, 6]];

var nestedStructure = _.reduceRight(nestedArrays, (result, array) => [array, result], []);

console.log(JSON.stringify(nestedStructure));

Output: 

"[[5,6],[[3,4],[[1,2],[]]]]"


An example of building a nested array structure in reverse.

Frequently Asked Questions 

How does _.reduceRight() differ from _.reduce()?

While _.reduce() iterates from the start to the end of a collection, _.reduceRight() performs the iteration in reverse, from the end to the start.

Can _.reduceRight() be used with non-array collections?

Yes, it can be used with objects, but it's most commonly and effectively used with arrays, especially when the order of elements is significant.

Is _.reduceRight() efficient for large collections?

_.reduceRight() is generally efficient, but its performance depends on the complexity of the iteratee function and the size of the collection.

Conclusion

Lodash's _.reduceRight() method is a functional and efficient way to accumulate values from a collection in reverse order. It provides a flexible approach to perform reduction operations on collections, especially useful in scenarios where the processing order is crucial.

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