Windows Dev Center

List.reduce method

Applies to Windows and Windows Phone

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 supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Minimum supported phone

Windows Phone 8.1

Namespace

WinJS.Binding

Library

Winjs.js

See also

List

 

 

Show:
© 2015 Microsoft