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
廣告