mozilla
Your Search Results

    Array reduce method Redirect 1

    Summary

    The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) has to reduce it to a single value.

    Syntax

    arr.reduce(callback,[initialValue])

    Parameters

    callback
    Function to execute on each value in the array, taking four arguments:
    previousValue
    The value previously returned in the last invocation of the callback, or initialValue, if supplied. (See below.)
    currentValue
    The current element being processed in the array.
    index
    The index of the current element being processed in the array.
    array
    The array reduce was called upon.
    initialValue
    Object to use as the first argument to the first call of the callback.

    Description

    reduce 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 first time the callback is called, previousValue and currentValue can be one of two values. If initialValue is provided in the call to reduce, then previousValue will be equal to initialValue and currentValue will be equal to the first value in the array. If no initialValue was provided, then previousValue will be equal to the first value in the array and currentValue will be equal to the second.

    Suppose the following use of reduce occurred:

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

    The callback would be invoked four times, with the arguments and return values in each call being as follows:

      previousValue currentValue index array return value
    first call 0 1 1 [0,1,2,3,4] 1
    second call 1 2 2 [0,1,2,3,4] 3
    third call 3 3 3 [0,1,2,3,4] 6
    fourth call 6 4 4 [0,1,2,3,4] 10

    The value returned by reduce would be that of the last callback invocation (10).

    If you were to provide an initial value as the second argument to reduce, the result would look like this:

    [0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
      return previousValue + currentValue;
    }, 10);
    
      previousValue currentValue index array return value
    first call 10 0 0 [0,1,2,3,4] 10
    second call 10 1 1 [0,1,2,3,4] 11
    third call 11 2 2 [0,1,2,3,4] 13
    fourth call 13 3 3 [0,1,2,3,4] 16
    fifth call 16 4 4 [0,1,2,3,4] 20

    The value returned by reduce this time would be, of course, 20.

    Examples

    Example: Sum up all values within an array

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

    Example: Flatten an array of arrays

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

    Polyfill

    Array.prototype.reduce was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of reduce in implementations which do not natively support it.

    if ('function' !== typeof Array.prototype.reduce) {
      Array.prototype.reduce = function(callback, opt_initialValue){
        'use strict';
        if (null === this || 'undefined' === typeof this) {
          // At the moment all modern browsers, that support strict mode, have
          // native implementation of Array.prototype.reduce. For instance, IE8
          // does not support strict mode, so this check is actually useless.
          throw new TypeError(
              'Array.prototype.reduce called on null or undefined');
        }
        if ('function' !== typeof callback) {
          throw new TypeError(callback + ' is not a function');
        }
        var index, value,
            length = this.length >>> 0,
            isValueSet = false;
        if (1 < arguments.length) {
          value = opt_initialValue;
          isValueSet = true;
        }
        for (index = 0; length > index; ++index) {
          if (this.hasOwnProperty(index)) {
            if (isValueSet) {
              value = callback(value, this[index], index, this);
            }
            else {
              value = this[index];
              isValueSet = true;
            }
          }
        }
        if (!isValueSet) {
          throw new TypeError('Reduce of empty array with no initial value');
        }
        return value;
      };
    }
    

    Specifications

    Specification Status Comment
    ECMAScript Language Specification 5.1th Edition (ECMA-262) Standard Initial definition.
    Implemented in JavaScript 1.8
    ECMAScript Language Specification 6th Edition (ECMA-262) Draft  

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support (Yes) 3.0 (1.9) 9 10.5 4.0
    Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
    Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)

    See also

    Document Tags and Contributors

    Contributors to this page: Sheppy
    Last updated by: Sheppy,
    Hide Sidebar