Tutorials Point


  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint



  Home     References     Discussion Forums     About TP  

Javascript Array reduceRight() Method


previous next AddThis Social Bookmark Button


Advertisements

Description:

Javascript array reduceRight() method applies a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.

Syntax:

array.reduceRight(callback[, initialValue]);

Here is the detail of parameters:

  • callback : Function to execute on each value in the array.

  • initialValue : Object to use as the first argument to the first call of the callback.

Return Value:

Returns the reduceRightd single value of the array.

Compatibility:

This method is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. To make it work you need to add following code at the top of your script:

if (!Array.prototype.reduceRight)
{
  Array.prototype.reduceRight = function(fun /*, initial*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    // no value to return if no initial value, empty array
    if (len == 0 && arguments.length == 1)
      throw new TypeError();

    var i = len - 1;
    if (arguments.length >= 2)
    {
      var rv = arguments[1];
    }
    else
    {
      do
      {
        if (i in this)
        {
          rv = this[i--];
          break;
        }

        // if array contains no values, no initial value to return
        if (--i < 0)
          throw new TypeError();
      }
      while (true);
    }

    for (; i >= 0; i--)
    {
      if (i in this)
        rv = fun.call(null, rv, this[i], i, this);
    }

    return rv;
  };
}

Example:

<html>
<head>
<title>JavaScript Array reduceRight Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.reduceRight)
{
  Array.prototype.reduceRight = function(fun /*, initial*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    // no value to return if no initial value, empty array
    if (len == 0 && arguments.length == 1)
      throw new TypeError();

    var i = len - 1;
    if (arguments.length >= 2)
    {
      var rv = arguments[1];
    }
    else
    {
      do
      {
        if (i in this)
        {
          rv = this[i--];
          break;
        }

        // if array contains no values, no initial value to return
        if (--i < 0)
          throw new TypeError();
      }
      while (true);
    }

    for (; i >= 0; i--)
    {
      if (i in this)
        rv = fun.call(null, rv, this[i], i, this);
    }

    return rv;
  };
}

var total = [0, 1, 2, 3].reduceRight(function(a, b)
                                     { return a + b; });
document.write("total is : " + total ); 
</script>
</body>
</html>

This will produce following result:

total is : 6

To understand it in better way you can Try it yourself.



previous next Printer Friendly

Advertisements


  

Advertisements