when to use reduce and reduceRight?

前端 未结 6 2065
野性不改
野性不改 2020-12-17 08:44

Can you describe this for me?

var arr, total;
arr = [1, 2, 3, 4, 5];
total = arr.reduce(function(previous, current) {
return previous + current;
});
// total         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 09:20

    The only thing is order of computation. Reduce does it 'left to right', reduceRight does it 'right to left'. See the following example:

    
    
     
    
     
    
     
    
     
    
     
     

提交回复
热议问题