關於 Array.reduceRight()
前言
在之前的 關於 Array.reduce() 學習了 Array.reduce() ,這次來學習 Array.reduceRight() ,在此把學習的過程做個紀錄。
內容
範例如下
let ar = [ 1, 2, 3, 4 ];
let sum = ar.reduceRight(
( previousValue , currentValue) =>{
console.log(currentValue);
return previousValue+currentValue ;
},
0
);
// 4
// 3
// 2
// 1
console.log( `sum:${sum}`);//10
Array.reduceRight() 的用法和 Array.reduce() 是一樣的,差別是陣列瀏覽的順序是倒過來的!範例會在每次瀏覽時列印數值,可以知道是反著瀏覽陣列。
參考資料
[ developer.mozilla.org ] Array.prototype.reduceRight()
沒有留言:
張貼留言