JavaScript数组归并方法reduce
示例代码:
<!DOCTYPE html>
<html lang="zh">
<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>JavaScript数组归并方法</title>
</head>
<body>
<script>
var arr = [1, 2, 3, 4];
var sum = arr.reduce(function(total, cur, index, arr) {
console.log("total(初始值, 或者计算结束后的返回值): " + total);
console.log("cur(当前元素): " + cur);
console.log("index(当前元素的索引): " + index);
return total + cur;
});
console.log(sum)
</script>
</body>
</html>
作者:孟繁贵 Email:meng010387@126.com 期待共同进步!