-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path补充reduce.html
41 lines (32 loc) · 1.23 KB
/
补充reduce.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- -->
<script>
// 计算数组元素相加后的总和:
var numbers = [65, 44, 12, 4];
let getSum = (total, num) => total + num;
let myFunction = function (item) {
numbers.reduce(getSum);
}
console.log(numbers.reduce());
// reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,
// 最终计算为一个值。
/*
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
reduce() 可以作为一个高阶函数,用于函数的 compose。
注意: reduce() 对于空数组是不会执行回调函数的。
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
numbers.reduce(getSum);
getSum = (total, num);
function(total, currentValue, currentIndex, arr)
*/
</script>
</body>
</html>