Revision 29435 of Array.prototype.reduceRight()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/ReduceRight
  • Revision title: Array reduceRight method
  • Revision id: 29435
  • Created:
  • Creator: Jresig
  • Is current revision? No
  • Comment New page: <breadcrumbs></breadcrumbs> == Summary == Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value. <table class="fullwidth...

Revision Content

Summary

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

Method of Array
Implemented in: JavaScript 1.8 (Gecko 1.9a5 and later)
ECMAScript Edition: none

Syntax

var result = array.reduceRight(callback[, initialValue]);

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.

Description

reduceRight executes the callback function once for each element present in the array, receiving two arguments: The initial value (or value from the previous callback call) and the value of the current element.

The call to the reduceRight callback would look something like this:

.reduce(function(previousValue, currentValue){
  // ...
})

The first time the function is called, the previousValue and currentValue can be one of two values. If an initialValue was provided in the call to reduceRight, then previousValue will be equal to initialValue and currentValue will be equal to the last value in the array. If no initialValue was provided, then previousValue will be equal to the last value in the array and currentValue will be equal to the second-to-last value.

Some example run-throughs of the function would look like this:

[0,1,2,3,4].reduceRight(function(previousValue, currentValue){
  return previousValue + currentValue;
});

// First call
previousValue = 4, currentValue = 3

// Second call
previousValue = 7, currentValue = 2

// Third call
previousValue = 9, currentValue = 1

// Fourth call
previousValue = 10, currentValue = 0

// Return Value: 10

And if you were to provide an initialValue, the result would look like this:

[0,1,2,3,4].reduceRight(function(previousValue, currentValue){
  return previousValue + currentValue;
}, 10);

// First call
previousValue = 10, currentValue = 4

// Second call
previousValue = 14, currentValue = 3

// Third call
previousValue = 17, currentValue = 2

// Fourth call
previousValue = 19, currentValue = 1

// Fifth call
previousValue = 20, currentValue = 0

// Return Value: 20

Compatibility

reduceRight is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of reduceRight in ECMA-262 implementations which do not natively support it.

if (!Array.prototype.reduceRight)
{
  Array.prototype.reduceRight = function(fn, initial)
  {
    if (typeof fn != "function")
      throw new TypeError();
    
    if (typeof initial != "undefined")
      var start = this.length - 1, val = initial;
    else
      var start = this.length - 2, val = this[ this.length - 1 ];
    
    for (var i = start; i >= 0; i--)
    {
      val = fn.call(this, val, this[i]);
    }
    
    return val;
  };
}

Examples

Example: Sum up all values within an array

array.reduceRight(function(a,b){ return a+b; });

With some input like this:

[ 0, 1, 2, 3 ]

the output would be thus:

6

Example: Flatten an array of arrays

array.reduceRight(function(a,b){
  return a.concat( b );
}, []);

With some input like this:

[ [0,1], [2,3], [4,5] ]

the output would be thus:

[ 0, 1, 2, 3, 4, 5 ]

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/Array">Array</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.8 (Gecko 1.9a5 and later)</td>
</tr>
<tr>
<td>ECMAScript Edition:</td>
<td>none</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval">var <i>result</i> = <i>array</i>.reduceRight(<i>callback</i>[, <i>initialValue</i>]);
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>callback</code> </dt><dd> Function to execute on each value in the array.
</dd><dt> <code>initialValue</code> </dt><dd> Object to use as the first argument to the first call of the <code>callback</code>.
</dd></dl>
<h3 name="Description"> Description </h3>
<p><code>reduceRight</code> executes the <code>callback</code> function once for each element present in the array, receiving two arguments: The initial value (or value from the previous <code>callback</code> call) and the value of the current element.
</p><p>The call to the reduceRight <code>callback</code> would look something like this:
</p>
<pre class="eval">.reduce(function(previousValue, currentValue){
  // ...
})
</pre>
<p>The first time the function is called, the <code>previousValue</code> and <code>currentValue</code> can be one of two values. If an <code>initialValue</code> was provided in the call to <code>reduceRight</code>, then <code>previousValue</code> will be equal to <code>initialValue</code> and <code>currentValue</code> will be equal to the last value in the array. If no <code>initialValue</code> was provided, then <code>previousValue</code> will be equal to the last value in the array and <code>currentValue</code> will be equal to the second-to-last value.
</p><p>Some example run-throughs of the function would look like this:
</p>
<pre class="eval">[0,1,2,3,4].reduceRight(function(previousValue, currentValue){
  return previousValue + currentValue;
});

// First call
previousValue = 4, currentValue = 3

// Second call
previousValue = 7, currentValue = 2

// Third call
previousValue = 9, currentValue = 1

// Fourth call
previousValue = 10, currentValue = 0

// Return Value: 10
</pre>
<p>And if you were to provide an <code>initialValue</code>, the result would look like this:
</p>
<pre class="eval">[0,1,2,3,4].reduceRight(function(previousValue, currentValue){
  return previousValue + currentValue;
}, 10);

// First call
previousValue = 10, currentValue = 4

// Second call
previousValue = 14, currentValue = 3

// Third call
previousValue = 17, currentValue = 2

// Fourth call
previousValue = 19, currentValue = 1

// Fifth call
previousValue = 20, currentValue = 0

// Return Value: 20
</pre>
<h3 name="Compatibility"> Compatibility </h3>
<p><code>reduceRight</code> is a JavaScript extension to the ECMA-262 standard; as such it may not be present in other implementations of the standard.  You can work around this by inserting the following code at the beginning of your scripts, allowing use of <code>reduceRight</code> in ECMA-262 implementations which do not natively support it.
</p>
<pre class="eval">if (!Array.prototype.reduceRight)
{
  Array.prototype.reduceRight = function(fn, initial)
  {
    if (typeof fn != "function")
      throw new TypeError();
    
    if (typeof initial != "undefined")
      var start = this.length - 1, val = initial;
    else
      var start = this.length - 2, val = this[ this.length - 1 ];
    
    for (var i = start; i &gt;= 0; i--)
    {
      val = fn.call(this, val, this[i]);
    }
    
    return val;
  };
}
</pre>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Sum_up_all_values_within_an_array"> Example: Sum up all values within an array </h4>
<pre class="eval">array.reduceRight(function(a,b){ return a+b; });
</pre>
<p>With some input like this:
</p>
<pre class="eval">[ 0, 1, 2, 3 ]
</pre>
<p>the output would be thus:
</p>
<pre class="eval">6
</pre>
<h4 name="Example:_Flatten_an_array_of_arrays"> Example: Flatten an array of arrays </h4>
<pre class="eval">array.reduceRight(function(a,b){
  return a.concat( b );
}, []);
</pre>
<p>With some input like this:
</p>
<pre class="eval">[ [0,1], [2,3], [4,5] ]
</pre>
<p>the output would be thus:
</p>
<pre class="eval">[ 0, 1, 2, 3, 4, 5 ]
</pre>
Revert to this revision