打赏

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>

 

posted @ 2018-03-26 11:57  孟繁贵  阅读(302)  评论(0编辑  收藏  举报
TOP