TypeScript Array reduceRight()
Syntax & Examples
Array.reduceRight() method
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.
Syntax of Array.reduceRight()
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
.
✐ Examples
1 Using reduceRight() to sum an array of numbers
In TypeScript, you can use the reduceRight()
method to sum all the numbers in an array, starting from the last element.
For example,
- Define an array
numbers
with some initial values. - Use the
reduceRight()
method with a callback function that adds the previous and current values. - Store the result in the variable
sum
. - Log
sum
to the console using theconsole.log()
method.
TypeScript Program
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduceRight((previousValue, currentValue) => previousValue + currentValue);
console.log(sum);
Output
15
2 Using reduceRight() with an initial value to calculate the product
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,
- Define an array
numbers
with some initial values. - Use the
reduceRight()
method with a callback function that multiplies the previous and current values, and an initial value of 1. - Store the result in the variable
product
. - Log
product
to the console using theconsole.log()
method.
TypeScript Program
const numbers = [1, 2, 3, 4, 5];
const product = numbers.reduceRight((previousValue, currentValue) => previousValue * currentValue, 1);
console.log(product);
Output
120
3 Using reduceRight() to flatten a nested array
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,
- Define a nested array
nestedArray
with some initial values. - Use the
reduceRight()
method with a callback function that concatenates the previous and current values. - Store the result in the variable
flattenedArray
. - Log
flattenedArray
to the console using theconsole.log()
method.
TypeScript Program
const nestedArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = nestedArray.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue), []);
console.log(flattenedArray);
Output
[5, 6, 3, 4, 1, 2]
Summary
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.