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
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5];
var sum = _.reduceRight(numbers, (sum, n) => sum + n, 0);
console.log(sum);

You can also try this code with Online Javascript Compiler
Run Code
Output:
15
Demonstrates adding up the numbers in an array, starting from the end.
Concatenating String Elements in Reverse Order:
JavaScript
var strings = ['hello', 'world', 'lodash'];
var concatenated = _.reduceRight(strings, (result, str) => result + ' ' + str, '');
console.log(concatenated);

You can also try this code with Online Javascript Compiler
Run Code
Output:
'lodash world hello'
Shows concatenation of strings in an array, starting from the last element.
Complex Reduction with Nested Structures:
JavaScript
var collections = [{ 'count': 5 }, { 'count': 10 }, { 'count': 15 }];
var totalCount = _.reduceRight(collections, (sum, obj) => sum + obj.count, 0);
console.log(totalCount);

You can also try this code with Online Javascript Compiler
Run Code
Output:
30
Demonstrates summing values from a collection of objects in reverse order.
Building a Nested Structure in Reverse:
JavaScript
var nestedArrays = [[1, 2], [3, 4], [5, 6]];
var nestedStructure = _.reduceRight(nestedArrays, (result, array) => [array, result], []);
console.log(JSON.stringify(nestedStructure));

You can also try this code with Online Javascript Compiler
Run Code
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 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.