0%

JavaScript 利用 reduce()函数求数组的和,积, 最大值

求数组的和

1
2
var a[1,2,3,4,5];
var sum = a.reduce(function(x,y){return x+y}, 0);

求数组的积

1
2
var a[1,2,3,4,5];
var product = a.reduce(function(x,y){return x*y}, 1);

求数组中最大值

1
2
var a[1,2,3,4,5];
var max = a.reduce(function(x,y){return (x>y)?x:y});