reduce

Summary

Applies an accumulator function (on each array element from left to right) to reduce the array to a single value.

Signatures

Click on a signature to select it and view its documentation.

Usage

public T reduce(T(T previousValue) callback)

Returns

The reduced value.

Parameters

callback

A function which provides the operation logic.

Description

Applies an accumulator function (on each array element from left to right) to reduce the array to a single value.

This method was standardized in ECMAScript 5 for JavaScript. For web browsers that do not support ECMAScript 5, JS++ will provide a polyfill for this method only if it is used.

Examples

Sum all values
1
2
3
4
5
6
7
import System;
 
int[] numbers = [ 2, 4, 6, 8, 10 ];
int sum = numbers.reduce(int(int previousValue, int currentValue) {
    return previousValue + currentValue;
});
Console.log(sum); // 30

Share

HTML | BBCode | Direct Link