高阶函数 filter map reduce
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的更多相关文章
- python 函数式编程:高阶函数,map/reduce
python 函数式编程:高阶函数,map/reduce #函数式编程 #函数式编程一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数 #(一)高阶函数 f=abs f print ...
- js基础--高阶函数(map,reduce,filter,sort)
高阶函数 一个函数可以接收另一个函数作为参数,这种函数就称之为高阶函数,编写高阶函数,就是让函数的参数能够接收别的函数. function add (x,y,f){return f(x)+f(y)} ...
- Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)...啊啊啊
函数式编程 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计 ...
- (转)Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)
原文:https://www.cnblogs.com/chenwolong/p/reduce.html 函数式编程 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数 ...
- [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 ...
- Python学习笔记系列——高阶函数(map/reduce)
一.map #变量可以指向函数,函数的参数能接受变量,那么一个函数就可以接受另一个函数作为参数,这种函数被称之为高阶函数 def add(x,y,f): return f(x)+f(y) print( ...
- 高阶函数:map()/reduce()
Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clus ...
- python的高阶函数(map,reduce,filter)
Map函数 Map()函数接受两个参数,第一个参数是函数,第二个参数是序列(list,tuple),map将函数依次作用到序列上的每一个元素上,并发结果作为新的list返回 其中map的第一个参数的函 ...
- js 高阶函数(map/reduce/filter/sort)
1.map - 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值(注:map不会对空数组进行检测,不会改变原始数组) 语法:array.map(function(currentValu ...
随机推荐
- 模拟I2C协议学习点滴之程序相关定义
由于主机和从机都会给数据线SDA发信号,比如主机先给SDA发送数据后,从机收到数据后发送应答信号将SDA拉低,故SDA类型设定为inout.而DATA设定为inout类型,是起到校验通信的作用(后续的 ...
- 怎么联系$zcy$呢?
\(QQ:2939533969\) \(luogu:\)little_sun 窝经常以little_sun,little_sun0331,zcy05331的昵称混迹于各大网站 窝的CSDN blog ...
- CodeForces 750A New Year and Hurry
#include<bits/stdc++.h> using namespace std; int main() { int n, k, i, sum; while(~scanf(" ...
- c++ 珊格迷宫问题
#demo1 #include<iostream> #include<ctime> #include<cstdlib> #include<queue> ...
- elasticsearch update方法报错: Too many dynamic script compilations within, max: [75/5m]
PUT _cluster/settings { "transient" : { "script.max_compilations ...
- zabbix (二)安装
一.centos7源码安装zabbix3.x 1.安装前环境搭建 下载最新的yum源 #wget -P /etc/yum.repos.d http://mirrors.aliyun.com/repo/ ...
- x2goserver 连接问题
The remote proxy closed the connection while negotiating the session. This may be due to the wrong a ...
- dubbo+zookeeper搭建时报错java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy
说一下我的环境: jdk1.8 dubbo2.6.1 zookeeper3.4.10 maven3.3.9 搭建demo时报错:java.lang.NoClassDefFoundError: org/ ...
- qt QThread
QThread类提供了一个平台无关的方式来管理线程. 一个QThread对象在程序控制中管理一个线程.线程在run()中开始执行.默认情况下,run()通过调用exec()启动事件循环并在线程里运行一 ...
- 接口操作XML
接口操作XML 以下代码旨在 脱离TXMLDocument 操作 xml. unit Unit3; interface uses Windows, Messages, SysUtils, Varian ...