-
Notifications
You must be signed in to change notification settings - Fork 15
JSArray.Reduce
boxgaming edited this page Jul 25, 2026
·
2 revisions
Executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
JSArray.Reduce a, callbackFn [,initialValue]
- The a parameter contains the native Javascript array on which the operation will be performed.
- The callbackFn parameter specifies a function to execute for each element in the array. Its return value becomes the value of the accumulator parameter on the next invocation of callbackFn. For the last invocation, the return value becomes the return value of Reduce. The function is called with the following arguments:
-
accumulator
The value resulting from the previous call to callbackFn. On the first call, its value is initialValue if the latter is specified; otherwise, its value is set to the first element in the array. -
currentValue
The value of the current element. On the first call, its value is the first array element if initialValue is specified; otherwise, its value is set to the second array element. -
currentIndex
The index position of currentValue in the array. On the first call, its value is 0 if initialValue is specified, otherwise 1. - array The array Reduce was called upon.
-
accumulator
- If the optional initialValue is specified, callbackFn starts executing with the first value in the array as currentValue. If initialValue is not specified, accumulator is initialized to the first value in the array, and callbackFn starts executing with the second value in the array as currentValue.
Import JSArray From "lib/lang/array.bas"
Dim numbers As Object
numbers = JSArray.Create(5, 27, 18, 9, 4, 22, 20)
Print JSArray.Reduce(numbers, @Sum)
Print JSArray.Reduce(numbers, @Sum, 100)
Function Sum (accumulator, currentValue)
Sum = accumulator + currentValue
End Function105
205
JSArray.Create
JSArray.ForEach
JSArray.ReduceRight
Javascript - Array.reduce()