Share via


List.reduce method

Accumulates a single result by calling the specified callback function for all elements in a list. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.

Syntax

var object = list.reduce(callback, initiallValue);

Parameters

  • callback
    Type: Function

    A function that accepts up to four arguments. These arguments are:

    • previousValue: The value from the previous call to the callback function. If an initialValue is provided to the reduce method, the previousValue is initialValue the first time the function is called.

    • currentValue: the value of the current array element.

    • currentIndex: The value of the current array element.

    • array: The array object that contains the element.

    The function is called for each element in the list.

  • initiallValue
    Type: Object

    If initialValue is specified, it is used as the value with which to start the accumulation. The first call to the function provides this value as an argument instead of a list value.

Return value

Type: Object

The return value from the last call to the callback function.

Examples

The following code shows how to use the reduce function.

// A function that creates a number of the concatenated values of the array. 
function addDigitValue(previousValue, currentDigit, currentIndex, array) {
    var exponent = (array.length - 1) - currentIndex;
    var digitValue = currentDigit * Math.pow(10, exponent);
    return previousValue + digitValue;
}

var digits = [4, 1, 2, 5];

var list = new WinJS.Binding.List(digits);
        
var result = list.reduce(addDigitValue, 0);

// result is 4125

Requirements

Minimum WinJS version

WinJS 1.0

Namespace

WinJS.Binding

See also

List