array.reduceRight()
This method operates on an array. You typically fetch documents from a collection as a set, not an array. For the equivalent set method, see set instance methods. For differences between sets and arrays, see Sets vs. arrays. |
Reduce an array to a single, accumulated value by applying a function to each element. Iterates through elements from right to left. Uses the first element as the initial value.
Description
The reduceRight()
method iterates through each element in an array to perform
a rolling operation. For example, you can use reduceRight()
to calculate a
rolling sum, concatenate elements, or perform complex transformations.
reduceRight()
calls a reducer callback function on each element of the
array from right to left. The reducer function takes two
arguments:
-
The accumulator that holds the running result from previous iterations. The last element in the array serves as the initial accumulator.
-
The current element’s value from the array.
The method returns the result of the last iteration. The calling array isn’t changed.
Fold family methods
FQL supports several methods for folds, which iteratively reduce an array to a single value. These methods include:
The methods are similar but have the following differences:
-
array.fold()
andarray.foldRight()
accept an initial seed value and use it as the initial accumulator.array.reduce()
andarray.reduceRight()
use the array’s first element as the initial accumulator. -
array.fold()
andarray.reduce()
iterate through the array’s elements from left to right.array.foldRight()
andarray.reduceRight()
iterate through the array’s elements from right to left.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
reducer |
Yes |
Anonymous FQL function to call on each array element. |
Examples
Reduce the Array elements, right to left:
["A", "B", "C"].reduceRight((prev, cur) => prev + cur)
"CBA"
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!