reduce

Modified on Mon, 07 Aug 2023

Description

Apply an expression against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

Syntax

reduce(array, total, element, expression)

Parameters

Input ParameterTypeDescription
arrayArrayThe array reduce() was called upon
totalnumberThe initialValue, or the previously returned value of the expression. Normally, array element 0 is used as initial value, and the iteration starts from array element 1. If an initial value is supplied, this is used, and the iteration starts from array element 0.
elementanyThe current element being processed in the array
expressionexpressionAn expression to be run for each array element. Its return value becomes the value of the accumulator parameter on the next invocation of expression

Return value

Type
number

Sample

ExampleResultDescription
reduce([ 1, 2, 3 ], total, x, total+x)6Sum of an array elements with out any initial offset
reduce([ 1, 2, 3 ], 10, x, total+x)16Sum of an array with the initial offset value of 10

To view all the List functions, click here.