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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
        
    <head>
        <script type="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/
                          libs/underscore.js/1.9.1/underscore-min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/
                          libs/underscore.js/1.9.1/underscore-min.js.map"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/
                          libs/underscore.js/1.9.1/underscore.js"></script>
    </head>
        
    <body>
        <script type="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_right

    
    

    Output:

  2. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <script type="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/
                         libs/underscore.js/1.9.1/underscore-min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/
                         libs/underscore.js/1.9.1/underscore-min.js.map"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/
                         libs/underscore.js/1.9.1/underscore.js"></script>
    </head>
       
    <body>
        <script type="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_right

    
    

    Output:

  3. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <script type="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/
              libs/underscore.js/1.9.1/underscore-min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/
              libs/underscore.js/1.9.1/underscore-min.js.map"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/
              libs/underscore.js/1.9.1/underscore.js"></script>
    </head>
       
    <body>
        <script type="text/javascript">
              var number=_.reduceRight([1, 2, 3, 4, 5],
                       function(memo, num) {
                  return num;
              });
              document.write(number);   
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:

  4. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <html>
       
    <head>
        <script type="text/javascript" src ="https://cdnjs.cloudflare.com/ajax/
                    libs/underscore.js/1.9.1/underscore-min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/
                    libs/underscore.js/1.9.1/underscore-min.js.map"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/
                    libs/underscore.js/1.9.1/underscore.js"></script>
    </head>
       
    <body>
        <script type="text/javascript">
              var sum=[0, 1, 2, 3, 4].reduceRight(function(a, c ) {
              return a + c;
              });
              document.write(sum);  
        </script>
    </body>
       
    </html>

    chevron_right

    
    

    Output:



My Personal Notes arrow_drop_up

I like to do coding in C++C and java programming languages HTML and CSS always intersts me Sharing knowleged is the best way according to me to increase ones knwoledge

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.