const app=new Vue({
el:'#app',
data:{
books:[{
id:1,
name:"算法导论",
data: '2006-1',
price:39.00,
count:1
},{
id:2,
name:"算法导论",
data: '2006-1',
price:39.00,
count:1
},{
id:3,
name:"算法导论",
data: '2006-1',
price:39.00,
count:1
},{
id:4,
name:"算法导论",
data: '2006-1',
price:39.00,
count:1
}]
},
methods:{
getfinalprice(price){
return "$"+price.toFixed(2);
},
decreasement(index){
this.books[index].count--;
},
increasment(index){
this.books[index].count++;
},
remove(index){
this.books.splice(index,1);
}
},
filters:{
getp(price){
return "$"+price.toFixed(2);
}
},
computed:{
totalPrice(){
// let result=0;
//1普通for循环
// for (let i=0;i<this.books.length;i++){
// result+=this.books[i].price*this.books[i].count;
// }
// return result;
// 2for(let i in this.books)
// for (let i in this.books){
// console.log(i);//可以获得下标的索引值
// }
// 3直接拿到books的每一项
// let totalprice=0;
// for (let item of this.books){
// totalprice+=item.price*item.count;
// }
// return totalprice; // 4高阶函数
/*
* filter map reduce
* filter中的回调函数有一个要求,必须返回一个布尔值;符合条件则会打印在一个新的数组中
* true:函数内部会自动将这次回调的n加入到新的数组中
* false:当返回false时,函数内部就会过滤掉这次的n
*
* */
// const nums=[122,2,3,43,53,6,7,8,];
// let newnum= nums.filter(function (n) {
// return n<100;
// });
// console.log(newnum)
// //map函数的使用 特点直接进行计算;
// let new2=nums.map(function (n) {
// return n*2;
// });
// console.log(new2); // reduce函数的使用 作用:对数组中的所有内容进行汇总;
// const nums=[122,2,3,43,53,6,7,8];
// let num3=nums.reduce(function (prevalue,n){
// return prevalue +n;
// },0);
// return num3;
// 使用高阶函数进行实现
// const nums=[122,2,3,43,53,6,7,8];
// let total=nums.filter(function (n) {
// return n<100;
// }).map(function (n) {
// return n*2;
// }).reduce(function (prevalue,n) {
// return prevalue+n;
// },0);
// console.log(total); return this.books.reduce(function (prevalue,book) {
return prevalue+book.price*book.count ;
},0) }
}
});

高阶函数 filter map reduce的更多相关文章

  1. python 函数式编程:高阶函数,map/reduce

    python 函数式编程:高阶函数,map/reduce #函数式编程 #函数式编程一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数 #(一)高阶函数 f=abs f print ...

  2. js基础--高阶函数(map,reduce,filter,sort)

    高阶函数 一个函数可以接收另一个函数作为参数,这种函数就称之为高阶函数,编写高阶函数,就是让函数的参数能够接收别的函数. function add (x,y,f){return f(x)+f(y)} ...

  3. Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)...啊啊啊

    函数式编程 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计 ...

  4. (转)Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)

    原文:https://www.cnblogs.com/chenwolong/p/reduce.html 函数式编程 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数 ...

  5. [py][lc]python高阶函数(匿名/map/reduce/sorted)

    匿名函数 - 传入列表 f = lambda x: x[2] print(f([1, 2, 3])) # x = [1,2,3] map使用 传入函数体 def f(x): return x*x r ...

  6. Python学习笔记系列——高阶函数(map/reduce)

    一.map #变量可以指向函数,函数的参数能接受变量,那么一个函数就可以接受另一个函数作为参数,这种函数被称之为高阶函数 def add(x,y,f): return f(x)+f(y) print( ...

  7. 高阶函数:map()/reduce()

    Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clus ...

  8. python的高阶函数(map,reduce,filter)

    Map函数 Map()函数接受两个参数,第一个参数是函数,第二个参数是序列(list,tuple),map将函数依次作用到序列上的每一个元素上,并发结果作为新的list返回 其中map的第一个参数的函 ...

  9. js 高阶函数(map/reduce/filter/sort)

    1.map - 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值(注:map不会对空数组进行检测,不会改变原始数组) 语法:array.map(function(currentValu ...

随机推荐

  1. dumpe/dumpe2fs/e2fsck

    xt2/3/4文件系统备份工具 导出ext2/ext3/ext4文件系统信息 dumpe2fs e2fsck 强制检查文件系统 检查文件系统

  2. mac apache 管理

    sudo apachectl start sudo apachectl stop sudo apachectl restart

  3. redis消息提醒设计方案细则

    ----需求明细: 现有多个角色,角色间需要交互,内容分为申请,审核通过,拒绝,解除合作.每个角色进入自己后台显示查看其他角色的未读消息个数,点击进入显示所有发送来的内容.最开始只存储未读个数,并不知 ...

  4. 配置centos7阿里镜像源和epel源

    [root@runstone yum.repos.d]# pwd /etc/yum.repos.d [root@runstone yum.repos.d]# cat aliBase.repo #镜像源 ...

  5. mysql5.7 源码安装步骤

    操作系统:centos 7 mysql版本:5.7  下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-gli ...

  6. kubernetes使用本地仓库

    k8s与docker的安装 我参考的是 https://kuboard.cn/install/install-k8s.html#%E6%96%87%E6%A1%A3%E7%89%B9%E7%82%B9 ...

  7. Sentinel控制台前端开发环境搭建

    Sentinel:分布式系统的流量防卫兵. 官网:https://sentinelguard.io Github:https://github.com/alibaba/sentinel Wiki:ht ...

  8. swoole的websockte例子

    服务器的环境,建议用bt.cn的一键环境 服务端: <?php /** * Created by PhpStorm. * User: Administrator * Date: 2019\5\2 ...

  9. <JavaScript>调用apply报错:CreateListFromArrayLike called on non-object;

    Function.apply(obj, args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args--> ...

  10. PorterDuffXfermode之PorterDuff.Mode.MULTIPLY

    package com.loaderman.customviewdemo.view; import android.content.Context; import android.graphics.B ...