Array.prototype.reduceRight()

  • Revision slug: JavaScript/Reference/Global_Objects/Array/ReduceRight
  • Revision title: Array reduceRight method
  • Revision id: 313097
  • Created:
  • Creator: trevorh
  • Is current revision? No
  • Comment Reorder compatability table

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
ECMAScript Edition ECMAScript 5th Edition

Syntax

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, excluding holes in the array, receiving four arguments: the initial value (or value from the previous callback call), the value of the current element, the current index, and the array over which iteration is occurring.

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

array.reduceRight(function(previousValue, currentValue, index, array) {
    // ...
});

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, index, array) {
    return previousValue + currentValue;
});

// First call
previousValue = 4, currentValue = 3, index = 3

// Second call
previousValue = 7, currentValue = 2, index = 2

// Third call
previousValue = 9, currentValue = 1, index = 1

// Fourth call
previousValue = 10, currentValue = 0, index = 0

// array is always the object [0,1,2,3,4] upon which reduceRight was called

// 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, index, array) {
    return previousValue + currentValue;
}, 10);

// First call
previousValue = 10, currentValue = 4, index = 4

// Second call
previousValue = 14, currentValue = 3, index = 3

// Third call
previousValue = 17, currentValue = 2, index = 2

// Fourth call
previousValue = 19, currentValue = 1, index = 1

// Fifth call
previousValue = 20, currentValue = 0, index = 0

// array is always the object [0,1,2,3,4] upon which reduceRight was called

// Return Value: 20

Compatibility

reduceRight is a recent addition 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 implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming Object and TypeError have their original value and that callbackfn.call evaluates to the original value of Function.prototype.call.

if (!Array.prototype.reduceRight)
{
  Array.prototype.reduceRight = function(callbackfn /*, initialValue */)
  {
    "use strict";

    if (this == null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof callbackfn != "function")
      throw new TypeError();

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

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

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

    while (k >= 0)
    {
      if (k in t)
        accumulator = callbackfn.call(undefined, accumulator, t[k], k, t);
      k--;
    }

    return accumulator;
  };
}

Examples

Example: Sum up all values within an array

var total = [0, 1, 2, 3].reduceRight(function(a, b) {
    return a + b;
});
// total == 6

Example: Flatten an array of arrays

var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
    return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]

See Also

Browser compatibility

Based on Kangax's compat tables

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatGeckoDesktop("1.9") }} 9 10.5 4.0
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

Revision Source

<h2 id="Summary">Summary</h2>
<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="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array" title="en-US/docs/JavaScript/Reference/Global_Objects/Array"><code>Array</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 5th Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax">Syntax</h2>
<p><code><em>array</em>.reduceRight(<em>callback</em>[, <em>initialValue</em>])</code></p>
<h2 id="Parameters">Parameters</h2>
<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>
<h2 id="Description">Description</h2>
<p><code>reduceRight</code> executes the callback function once for each element present in the array, excluding holes in the array, receiving four arguments: the initial value (or value from the previous callback call), the value of the current element, the current index, and the array over which iteration is occurring.</p>
<p>The call to the reduceRight <code>callback</code> would look something like this:</p>
<pre class="eval">
array.reduceRight(function(previousValue, currentValue, index, array) {
    // ...
});
</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="brush: js">
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
    return previousValue + currentValue;
});

// First call
previousValue = 4, currentValue = 3, index = 3

// Second call
previousValue = 7, currentValue = 2, index = 2

// Third call
previousValue = 9, currentValue = 1, index = 1

// Fourth call
previousValue = 10, currentValue = 0, index = 0

// array is always the object [0,1,2,3,4] upon which reduceRight was called

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

// First call
previousValue = 10, currentValue = 4, index = 4

// Second call
previousValue = 14, currentValue = 3, index = 3

// Third call
previousValue = 17, currentValue = 2, index = 2

// Fourth call
previousValue = 19, currentValue = 1, index = 1

// Fifth call
previousValue = 20, currentValue = 0, index = 0

// array is always the object [0,1,2,3,4] upon which reduceRight was called

// Return Value: 20
</pre>
<h2 id="Compatibility">Compatibility</h2>
<p><code>reduceRight</code> is a recent addition 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 implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming <code>Object</code> and <code>TypeError</code> have their original value and that <code>callbackfn.call</code> evaluates to the original value of <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/call" style="text-decoration: none; color: rgb(4, 137, 183) !important; cursor: default; " title="en-US/docs/JavaScript/Reference/Global Objects/Function/call">Function.prototype.call</a></code>.</p>
<!-- DO_NOT_MODIFY_THE_FOLLOWING_CODE_IN_ANY_WAY -->
<pre class="brush: js">
if (!Array.prototype.reduceRight)
{
  Array.prototype.reduceRight = function(callbackfn /*, initialValue */)
  {
    "use strict";

    if (this == null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length &gt;&gt;&gt; 0;
    if (typeof callbackfn != "function")
      throw new TypeError();

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

    var k = len - 1;
    var accumulator;
    if (arguments.length &gt;= 2)
    {
      accumulator = arguments[1];
    }
    else
    {
      do
      {
        if (k in this)
        {
          accumulator = this[k--];
          break;
        }

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

    while (k &gt;= 0)
    {
      if (k in t)
        accumulator = callbackfn.call(undefined, accumulator, t[k], k, t);
      k--;
    }

    return accumulator;
  };
}
</pre>
<h2 id="Examples">Examples</h2>
<h3 id="Example.3A_Sum_up_all_values_within_an_array">Example: Sum up all values within an array</h3>
<pre class="brush: js">
var total = [0, 1, 2, 3].reduceRight(function(a, b) {
    return a + b;
});
// total == 6
</pre>
<h3 id="Example.3A_Flatten_an_array_of_arrays">Example: Flatten an array of arrays</h3>
<pre class="brush: js">
var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
    return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
</pre>
<h2 id="See_Also">See Also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce" title="en-US/docs/Core_JavaScript_1.5_Reference/Global_Objects/Array/Reduce">reduce</a></li>
</ul>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>Based on <a class="external" href="http://kangax.github.com/es5-compat-table/">Kangax's compat tables</a></p>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatGeckoDesktop("1.9") }}</td>
        <td>9</td>
        <td>10.5</td>
        <td>4.0</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<!-- languages({
"es": "es/docs/Referencia_de_JavaScript_1.5/Objetos_globales/Array/reduceRight",
"fr": "fr/docs/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Array/reduceRight",
"ja": "ja/docs/JavaScript/Reference/Global_Objects/Array/reduceRight" }) -->
Revert to this revision