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);
//使用 reduceRight() 方法
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);
//使用 reduceRight() 方法
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() 方法对这个空类型数组 ([]) 从右到左的每个元素执行用户提供的名为"ArrowFunction"的 Reducer 回调函数。
<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);
//使用 reduce 方法
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

