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. jQuery相关方法10

    一.链式编程的原理 <script> //构造函数 function Person(age){ this.age=age; this.sayHi=function(txt){ if(txt ...

  2. xfce 快捷键 命令整理

    本文链接:https://blog.csdn.net/cFarmerReally/article/details/53375956 转载自:https://my.oschina.net/u/56535 ...

  3. ubuntu ukylin wineqq 登录时提示:您的号码暂时不能使用低版本的qq

    ubuntu ukylin wineqq 登录时提示:您的号码暂时不能使用低版本的qq,而有的qq号登录没有问题. 优麒麟官网上下载的qqwine安装包,解压后安装三个deb包. 郁闷了一下午,都想装 ...

  4. geometry_msgs/PoseStamped 类型的变量的构造

    #navpoint.msg geometry_msgs/PoseStamped target_pose uint8 floor uint8 type target_pose 的类型为geometry_ ...

  5. 三十三、DNS资源记录类型和请求流程

    DNS分布均衡(Load balance)的实现 在上级数据库中写两条记录(同一个名字对应对个IP时),DNS会自动将请求基于轮循方式,分给每个DNS服务器 例如: 第一次将请求给第一个DNS,第二次 ...

  6. ansible handlers

    示例:安装nginx --- - hosts: hadoop #指定主机组 remote_user: root #远程执行命令的用户 gather_facts: no #是否获取远程主机的信息 tas ...

  7. Tkinter 之RadioButton单选框标签

    一.参数说明 语法 作用 Radiobutton(root,text='xxxx') 单选框文本显示内容 Radiobutton(root,variable=color) 单选框索引变量,通过变量的值 ...

  8. vue中的父组件传值给子组件

    以上父组件以及父组件里面的代码 下面是子组件以及里面的代码

  9. 2018-2019-2 网络对抗技术 20165222 Exp 8 Web基础

    1.实践内容 (1).Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. 使用service apache2 start启 ...

  10. legend3---17、如何抽象和复用控制器中的方法

    legend3---17.如何抽象和复用控制器中的方法 一.总结 一句话总结: 比如不同的控制器中都用了检查手机号是否已经注册,是否没注册这样的功能,我应该如何抽象和复用方法 新建一个处理手机号码的模 ...