Skip to content

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.

Syntax

JSArray.ReduceRight a, callbackFn [,initialValue]

Parameters

  • 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.
  • 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.

Example

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 Function

Output

yellow|orange|blue|green|red
COLORS|5/5:yellow|4/5:orange|3/5:blue|2/5:green|1/5:red

See Also

JSArray.Create
JSArray.ForEach
JSArray.Reduce
Javascript - Array.reduceRight()

Clone this wiki locally