-
Notifications
You must be signed in to change notification settings - Fork 15
JSArray.ReduceRight
boxgaming edited this page Jul 25, 2026
·
3 revisions
Applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.
See also JSArray.Reduce for left-to-right.
JSArray.ReduceRight 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 colors As Object
colors = JSArray.Create("red", "green", "blue", "orange", "yellow")
Print JSArray.ReduceRight(colors, @ReverseConcat)
Print JSArray.ReduceRight(colors, @ReverseConcat2, "COLORS")
Function ReverseConcat (accumulator, currentValue)
ReverseConcat = accumulator + "|" + currentValue
End Function
Function ReverseConcat2 (accumulator, currentValue, index, array)
ReverseConcat2 = accumulator + "|" + (index+1) + "/" + array.length + ":" + currentValue
End Functionyellow|orange|blue|green|red
COLORS|5/5:yellow|4/5:orange|3/5:blue|2/5:green|1/5:red
JSArray.Create
JSArray.ForEach
JSArray.Reduce
Javascript - Array.reduceRight()