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 Parameter | Type | Description |
---|---|---|
array | Array | The array reduce() was called upon |
total | number | The 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. |
element | any | The current element being processed in the array |
expression | expression | An 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
Example | Result | Description |
---|---|---|
reduce([ 1, 2, 3 ], total, x, total+x) | 6 | Sum of an array elements with out any initial offset |
reduce([ 1, 2, 3 ], 10, x, total+x) | 16 | Sum of an array with the initial offset value of 10 |
To view all the List functions, click here.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article