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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。