array.reduceRight()是JavaScript中的内置函数,用于将给定数组的元素从右到左转换为单个值。
用法:
array.reduceRight(previousValue, currentValue)
参数:它接受两个参数previousValue和currentValue,它们表示给定输入数组的前一个和当前元素。
返回值:还原为单个值后返回结果。
代码1:
<script>
// Taking some array as the element of an array "A"
const A = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ];
// Calling array.reduceRight() function
a = A.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue));
// printing result
document.write(a);
</script>
输出:
7, 8, 4, 5, 6, 1, 2, 3
代码2:
<script>
// Taking some array as the element of an array "A"
const A = [ [ 1, 2, 3 ], [ "a", "b", "c" ], [ 7, 8 ] ];
// Calling array.reduceRight() function
a = A.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue));
// printing result
document.write(a);
</script>
输出:
7, 8, a, b, c, 1, 2, 3
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript | array.reduceRight()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。