js高阶函数的使用

多种循环方式:

totalPrice(){
            let totalPrice = 0
            // 1.普通for循环
            // for(let i=0; i < this.books.length; i++){
            //     totalPrice+=this.books[i].price * this.books[i].count
            // }

            // 2. i in books
            // for(let i in this.books){
            //     totalPrice+= this.books[i].price * this.books[i].count
            // }

            // 3. item in books
            for(let item of this.books){
                totalPrice+=item.price*item.count
            }

            return totalPrice
        }

编程范式:命令式编程/声明式编程
编程范式:面向对象编程(第一公民:对象)/函数式编程(第一公民:函数)

filter map reduce函数的使用

filter中的回调函数有一个要求,必须返回一个布尔值。
true:函数内部自动将回调的n加入新数组,false过滤掉

const nums = [11,333,444,4,55]

let newNum= nums.filter(function (n) {  
    return n<100
})

console.log(newNum)
// 2. map 

let new2Nums = newNum.map(function (n) {  
    return n * 2
})
console.log(new2Nums);

// 3. reduce 对数组中所有内容进行汇总

let total = new2Nums.reduce(function (preValue,n) {  
    return preValue + n
},0)

函数式编程


const nums = [10,333,444,20,40,50]


let total = nums.filter(function (n) {  
    return n < 100
}).map(function (n) {  
    return n *2
}).reduce(function (preValue,n) {  
    return preValue + n
})

console.log(total)

箭头函数:

const nums = [10,333,444,20,40,50]
let total = nums.filter(n => n < 100).map(n => n * 2).reduce((pre,n) => pre+n)
console.log(total)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值