js -- 高阶函数的使用

时间:2020-01-31 22:40:32   收藏:0   阅读:92
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<div id="app">

</div>
</body>
<script>
    let arr = [1,3,55,8];
    // 需求:arr数组中的所有小于5的值翻倍并求和
    let count = arr.filter(function(n){
        return n < 5;
    }).map(function(n){
        return n*2;
    }).reduce(function(preValue, n){
        return preValue + n;
    },0);
    console.log(`和:${count}`);
</script>
</html>

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!