The reduceRight() method applies a function against an accumulator and each element in the array (from right to left) to reduce it to a single value.
There are 3 variations for the syntax of Array.reduceRight() method. They are:
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackfn | required | A function that is called for each element in the array, taking four arguments: previousValue, currentValue, currentIndex, and array. |
This method executes the callback function for each element in the array, resulting in a single accumulated value. The first time the callback is called, previousValue is the last value in the array, and currentValue is the second-to-last value.
Returns value of type T
.
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackfn | required | A function that is called for each element in the array, taking four arguments: previousValue, currentValue, currentIndex, and array. |
initialValue | optional | A value to use as the first argument to the first call of the callback. |
This method executes the callback function for each element in the array, starting with the provided initialValue. The first time the callback is called, previousValue is initialValue, and currentValue is the last value in the array.
Returns value of type T
.
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackfn | required | A function that is called for each element in the array, taking four arguments: previousValue, currentValue, currentIndex, and array. |
initialValue | optional | A value to use as the first argument to the first call of the callback. |
This method executes the callback function for each element in the array, starting with the provided initialValue of type U. The first time the callback is called, previousValue is initialValue, and currentValue is the last value in the array.
Returns value of type U
.
In TypeScript, you can use the reduceRight()
method to sum all the numbers in an array, starting from the last element.
For example,
numbers
with some initial values.reduceRight()
method with a callback function that adds the previous and current values.sum
.sum
to the console using the console.log()
method.const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduceRight((previousValue, currentValue) => previousValue + currentValue);
console.log(sum);
15
In TypeScript, you can use the reduceRight()
method with an initial value to calculate the product of all numbers in an array, starting from the last element.
For example,
numbers
with some initial values.reduceRight()
method with a callback function that multiplies the previous and current values, and an initial value of 1.product
.product
to the console using the console.log()
method.const numbers = [1, 2, 3, 4, 5];
const product = numbers.reduceRight((previousValue, currentValue) => previousValue * currentValue, 1);
console.log(product);
120
In TypeScript, you can use the reduceRight()
method to flatten a nested array into a single array, starting from the last nested array.
For example,
nestedArray
with some initial values.reduceRight()
method with a callback function that concatenates the previous and current values.flattenedArray
.flattenedArray
to the console using the console.log()
method.const nestedArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = nestedArray.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue), []);
console.log(flattenedArray);
[5, 6, 3, 4, 1, 2]
In this TypeScript tutorial, we learned about reduceRight() method of Array: the syntax and few working examples with output and detailed explanation for each example.