VOOZH about

URL: https://www.geeksforgeeks.org/typescript/typescript-array-reduceright-method/

⇱ TypeScript Array reduceRight() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TypeScript Array reduceRight() Method

Last Updated : 19 Jul, 2024

The Array.prototype.reduceRight() method is a built-in TypeScript function used to apply a reducer function to each element of the array, processing the elements from right to left, ultimately reducing the array to a single value.

Syntax:

array.reduceRight(callback[, initialValue])

Parameters

  • callback: The function to execute on each element in the array. It takes the following arguments:
    • previousValue: The value resulting from the previous call to the callback.
    • currentValue: The current element being processed.
    • currentIndex: The index of the current element being processed.
    • array: The array reduceRight was called upon.
  • initialValue (optional): The initial value to use as the first argument to the first call of the callback.

Return Value: This method returns the reduced right single value of the array. 

Examples of Array reduceRight() Method

Example 1: Reducing an Array to a Single Value

In this example, we use reduceRight() to subtract all elements of the array from the rightmost element to the leftmost element.

Output:

-3

Example 2: Calculating a Product in Reverse Order

In this example, reduceRight() is used to calculate the product of all elements in the array, starting from the rightmost element.

Output:

64

The reduceRight() method is a powerful tool for performing reductions on arrays from right to left. It can be used for various operations, such as summing, multiplying, and concatenating elements, making it a versatile method in TypeScript.

Comment
Article Tags:
Article Tags:

Explore