JavaScript | _.reduceRight() with Examples
The _.reduce() is an inbuilt function in JavaScript which is used to perform operation on each element of the list from right. When all the elements of the list are passed from right to left to the function/iteratee and no more elements remains then the _.reduceRight loop ends.
It applies a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
Difference between _.reduce() and _.reduceRight()
Code #1:
Below code show that _.right() will give output from left to right where as _.reduceRight() will give output from right to left.
<script> var a = ['1', '2', '3', '4', '5']; var r = a.reduce(function(a, b) { return a + b; }); var rr = a.reduceRight(function(a, b) { return a + b; }); document.write(r +"<br>"); document.write(rr); </script> |
Output:
12345 54321
Syntax:
_.reduceRight(list, function())
Parameters: It accepts two parameters which are specified below-
- list: It is the list containing some elements which are going to be accessed from right to left.
- function: It is the function which going to perform a operation to reduce the list’s element form right to left.
Return value: It returns the reduced element of the list form right to left.
JavaScript codes to show the working of _.reduceRight() function:
- Passing a list of numbers to _.reduceRight() function: The ._reduceRight() function takes the element from the list one by one and do the specified operations on the code. Like here the operation is concatenation of the elements of the list to form a new list. After concatenating all the elements, the reduceRight function ends.
<html><head>libs/underscore.js/1.9.1/underscore-min.js"></script>libs/underscore.js/1.9.1/underscore-min.js.map"></script>libs/underscore.js/1.9.1/underscore.js"></script></head><body><scripttype="text/javascript">var list = [[00, 11], [22, 33], [44, 55]];var answer = _.reduceRight(list, function(a, b){ return a.concat(b); }, []);document.write(answer);</script></body></html>chevron_rightfilter_noneOutput:

- Passing a list of characters to the _.reduceRight() function : Here also we are doing the same thing as done in the 1st example. The difference is that, the list id not of numbers rather than of characters. So, the final list will contain all the characters but in right to left order of the original list.
<html><head>libs/underscore.js/1.9.1/underscore-min.js"></script>libs/underscore.js/1.9.1/underscore-min.js.map"></script>libs/underscore.js/1.9.1/underscore.js"></script></head><body><scripttype="text/javascript">var list = [['a', 'b'], ['c', 'd'], ['e', 'f']];var answer = _.reduceRight(list, function(a, b){ return a.concat(b); }, []);document.write(answer);</script></body></html>chevron_rightfilter_noneOutput:

- Finding out the value of the last iteration: The ‘num’ variable is a variable which stores the values of the list elements. Therefore since we are returning the value at the end when the function gets over, so, this implies that the list is also over. And since the list is traversed from right to left so the result will be the leftmost element.
<html><head>libs/underscore.js/1.9.1/underscore-min.js"></script>libs/underscore.js/1.9.1/underscore-min.js.map"></script>libs/underscore.js/1.9.1/underscore.js"></script></head><body><scripttype="text/javascript">var number=_.reduceRight([1, 2, 3, 4, 5],function(memo, num) {return num;});document.write(number);</script></body></html>chevron_rightfilter_noneOutput:

- Applying arithmetic operators in the _.reduceRight() function: If we try to perform any arithmetic operation like addition etc on the list of the elements then the first element will be from the Rightmost side.
<html><head>libs/underscore.js/1.9.1/underscore-min.js"></script>libs/underscore.js/1.9.1/underscore-min.js.map"></script>libs/underscore.js/1.9.1/underscore.js"></script></head><body><scripttype="text/javascript">var sum=[0, 1, 2, 3, 4].reduceRight(function(a, c ) {return a + c;});document.write(sum);</script></body></html>chevron_rightfilter_noneOutput:

Recommended Posts:
- ES2015: Latest version of JavaScript
- Must use JavaScript Array Functions – Part 1
- Must use JavaScript Array Functions – Part 2
- Understanding variable scopes in JavaScript
- Must use JavaScript Array Functions – Part 3
- JavaScript : The Awesome Script
- JavaScript ES2015: Block Scoping
- New features of JavaScript Arrays with ES2015
- JavaScript Backend basics
- Advanced JavaScript Backend Basics
- JavaScript | dataView.getInt16()
- JavaScript | Adding minutes to Date object
- JavaScript | this Identifier
- JavaScript | RegExp toString() Method
- JavaScript | clearTimeout() & clearInterval() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



