Bagikan melalui


List.reduceRight method

Accumulates a single result by calling the specified callback function for all elements in a list, starting with the last member of the 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.reduceRight(callback, initialValue);

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.

  • initialValue
    Type: Object

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

Return value

Type: Object

The return value from the last call to callback function.

Examples

The following code shows how to use the reduceRight 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.reduceRight(addDigitValue, 0);

// result is 4125

Requirements

Minimum WinJS version

WinJS 1.0

Namespace

WinJS.Binding

See also

List