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:

1.
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T

Parameters

ParameterOptional/RequiredDescription
callbackfnrequiredA 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.

2.
reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T

Parameters

ParameterOptional/RequiredDescription
callbackfnrequiredA function that is called for each element in the array, taking four arguments: previousValue, currentValue, currentIndex, and array.
initialValueoptionalA 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.

3.
reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U

Parameters

ParameterOptional/RequiredDescription
callbackfnrequiredA function that is called for each element in the array, taking four arguments: previousValue, currentValue, currentIndex, and array.
initialValueoptionalA 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,

  1. Define an array numbers with some initial values.
  2. Use the reduceRight() method with a callback function that adds the previous and current values.
  3. Store the result in the variable sum.
  4. Log sum to the console using the console.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,

  1. Define an array numbers with some initial values.
  2. Use the reduceRight() method with a callback function that multiplies the previous and current values, and an initial value of 1.
  3. Store the result in the variable product.
  4. Log product to the console using the console.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,

  1. Define a nested array nestedArray with some initial values.
  2. Use the reduceRight() method with a callback function that concatenates the previous and current values.
  3. Store the result in the variable flattenedArray.
  4. Log flattenedArray to the console using the console.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.