JavaScript - TypedArray reduceRight() 方法



JavaScript TypedArray 的 reduceRight() 方法对累加器和当前类型化数组的每个值(从右到左)执行回调函数,以将其缩减为单个值。

如果未将初始值传递给此方法,它将使用类型化数组的最后一个元素作为初始值并跳过。如果在空类型化数组上调用 reduceRight(),它将抛出一个'TypeError' 异常。

reduceRight()reduce() 方法彼此非常相似,唯一的区别是迭代方向。reduceRight() 方法从右到左迭代类型化数组,而 reduce() 方法从左到右迭代。

语法

以下是 JavaScript TypedArray reduceRight() 方法的语法:

reduceRight(callbackFn, initialValue)

参数

此方法接受两个名为“callbackFn”和“initialValue”的参数,如下所述:

callbackFn - 用户提供的要在类型化数组的每个元素上执行的函数。

此 callbackFn 函数还接受四个参数,即“accumulator”、“currentValue”、“currentIndex”和“array”。以下是每个参数的说明:

  • accumulator - 此参数表示先前迭代产生的累积值。它以初始值(显式提供或数组的第一个元素)开始,并在每次迭代中更新。

  • currentValue - 类型化数组中当前元素的值。如果指定了 initialValue,则其值为最后一个元素,否则其值为倒数第二个元素。

  • currentIndex - currentValue 在类型化数组中的索引位置。

  • array - 调用 reduceRight() 方法的类型化数组。

initialValue - 首次调用回调函数时累加器参数初始化的值。

返回值

此方法返回归约产生的值。

示例

示例 1

在以下示例中,我们使用 JavaScript TypedArray 的 reduceRight() 方法从右到左在类型化数组的每个元素上执行名为 RighttoLeft() 的用户提供的回调函数。此函数返回此类型化数组元素 [10, 20, 30, 40, 50]。

<html>
<head>
   <title>JavaScript TypedArray reduceRight() Method</title>
</head>
<body>
   <script>
      //function
      function RighttoLeft(accumulator, currentValue){
         return `${accumulator}, ${currentValue}`;
      }
      const T_array = new Uint16Array([10, 20, 30, 40, 50]);
      document.write("Typed array: ", T_array);
      
      //using the reduceRight() method
      document.write("<br>The reversed typed array: ", T_array.reduceRight(RighttoLeft));
   </script>    
</body>
</html>

输出

此程序将从右到左返回类型化数组元素 [10, 20, 30, 40, 50],如下所示:

Typed array: 10, 20, 30, 40, 50
The reversed typed array: 50, 40, 30, 20, 10

示例 2

类型化数组中所有元素的乘积

在此示例中,我们使用 JavaScript TypedArray 的 reduceRight() 方法从右到左在类型化数组 [1, 2, 3, 4, 5] 的每个元素上执行名为 multi() 的用户提供的函数。multi() 函数相乘当前类型化数组中的所有元素。

<html>
<head>
   <title>JavaScript TypedArray reduceRight() Method</title>
</head>
<body>
   <script>
      //function
      function multi(accumulator, currentValue){
         return accumulator * currentValue;
      }
      const T_array = new Uint16Array([1, 2, 3, 4, 5]);
      document.write("Typed array: ", T_array);
      
      //using the reduceRight() method
      document.write("<br>The product of typed array elements: ", T_array.reduceRight(multi));
   </script>    
</body>
</html>

输出

以上程序返回类型化数组元素的乘积 120。

Typed array: 1,2,3,4,5
The product of typed array elements: 120

示例 3

如果我们将initialValue 参数传递给此方法,reduceRight() 方法也会在其上执行用户提供的回调函数,并返回单个值作为结果。

在以下示例中,我们使用 JavaScript TypedArray 的 **reduceRight()** 方法,对类型化数组 [10, 15, 20, 25, 30] 中的每个元素从右到左执行用户提供的名为 **sum()** 的回调函数,包括传递的 initialValue 参数值 **15**。sum() 函数 **累加**当前类型化数组中所有元素的总和。

<html>
<head>
   <title>JavaScript TypedArray reduceRight() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint16Array([10, 20, 30, 40, 50]);
      document.write("Typed array: ", T_array);
      const initialValue = 10;
      document.write("<br>Initial value: ", initialValue);
      let sum = T_array.reduce(function(accumulator, currentValue){
         return accumulator + currentValue;
      }, initialValue);
      document.write("<br>The sum of typed array elements(including initial value): ", sum);
   </script>    
</body>
</html>

输出

执行上述程序后,它将返回类型化数组中所有元素(包括初始值)的总和 -

Typed array: 10,15,20,25,30
Initial value: 10
The sum of typed array elements(including initial value): 160

示例 4

如果当前类型化数组不包含任何元素(没有可用的初始值),则 reduceRight() 方法将抛出 **"TypeError"** 异常。

在下面给出的示例中,我们使用 JavaScript TypedArray 的 **reduceRight()** 方法,对这个空类型化数组 **([])** 中的每个元素从右到左执行用户提供的 reducer 回调函数“ArrowFunction”。

<html>
<head>
   <title>JavaScript TypedArray reduceRight() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint16Array([]);//empty typed array
      document.write("Length of the typed array: ", T_array.length);
      
      //using the reduce method
      try {
         T_array.reduce((a, b)=> a + b);
      } catch (error) {
         document.write("<br>", error);
      }
   </script>    
</body>
</html>

输出

执行上述程序后,它将抛出 'TypeError' 异常。

Length of the typed array: 0
TypeError: Reduce of empty array with no initial value
广告

© . All rights reserved.