ES6 map与filter
ES6 map与filter
1、map
let arr1 = [1,2,3];
let arr2 = arr1.map((value,key,arr) => {
console.log(value) // 1,2,3
console.log(key) // 0,1,2
console.log(arr) //[1,2,3] [1,2,3] [1,2,3]
return value * value;
})
console.log(arr1); // [ 1, 2, 3 ]
console.log(arr2); // [ 1, 4, 9 ]
2、filter
filter 过滤函数,返回符合条件的元素数组。
let arr1 = [1,2,3];
let arr2 = arr1.filter((value,key,arr) => {
console.log(value) // 1,2,3
console.log(key) // 0,1,2
console.log(arr) // [1,2,3]
return value >= 3 ? false : true; //内部走的就是value>=3 成立吗,成立的部分返回,不成立的部分不返回。
})
console.log(arr1); // [ 1, 2, 3 ]
console.log(arr2); // [ 1, 2 ]
- 筛选符合条件项
//返回数组中大于等于 2的新数组
console.log("--------------筛选出(大于等于2的)-----------------------")
let arr = [1, 2, 3]
let newArr = arr.filter(item => item >= 2)
console.log(newArr)
- 数组去重
let arr5 = [1, 2, 2, 3, 4, 5, 5, 6];
let newArr4 = arr5.filter((x, index, self) => {
return self.indexOf(x) === index
})
console.log(newArr4) //[1,2,3,4,5,6]
- 去掉空字符串、undefined、null
//filter() 去掉空字符串、undefined、null
let arr4 = ['', '1', '2', undefined, '3.jpg', undefined]
let newArr3 = arr4.filter(item => item)
console.log(newArr3); //['1', '2', '3.jpg']
- 筛选数组对象
let arr = [
{a:'苹果',b:'面包',c:'吃'},
{a:'香蕉',b:'面包',c:'不吃'},
{a:'香蕉',b:'苹果',c:'吃'},
{a:'苹果',b:'香蕉',c:'不吃'},
]
let newarr8 = arr7.filter((value, index, arr) => {
return value.a != '苹果'
})
console.log(newarr8) //[{a:'香蕉',b:'面包',c:'不吃'},{a:'香蕉',b:'苹果',c:'吃'}]
let a = '苹果'; //筛选参数a
let b = '面包'; //筛选参数b
let c = '' //筛选参数c
let arr = [
{a:'苹果',b:'面包',c:'吃'},
{a:'香蕉',b:'面包',c:'不吃'},
{a:'香蕉',b:'苹果',c:'吃'},
{a:'苹果',b:'香蕉',c:'不吃'},
];
if (a != '') {
arr = arr.filter(item => item.a === a)
}
if (b != '') {
arr = arr.filter(item => item.b === b)
}
if (c != '') {
arr = arr.filter(item => item.c === c)
}
console.log(arr) // 筛选结果: [{a:'苹果',b:'面包',c:'吃'}]
ES6 map与filter的更多相关文章
- es6 map()和filter()详解【转】
原文地址:http://www.zhangxinxu.com/wordpress/2013/04/es5%e6%96%b0%e5%a2%9e%e6%95%b0%e7%bb%84%e6%96%b9%e6 ...
- ES6 数组遍历方法的实战用法总结(forEach,every,some,map,filter,reduce,reduceRight,indexOf,lastIndexOf)
目录 forEach every some map filter reduce && reduceRight indexOf lastIndexOf 前言 ES6原生语法中提供了非常多 ...
- (八)map,filter,flatMap算子-Java&Python版Spark
map,filter,flatMap算子 视频教程: 1.优酷 2.YouTube 1.map map是将源JavaRDD的一个一个元素的传入call方法,并经过算法后一个一个的返回从而生成一个新的J ...
- python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))
1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- [python基础知识]python内置函数map/reduce/filter
python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...
- python的 map,filter, reduce, enumerate
一, map #基本的map运用都可以用解析去替代,复杂的仍然需要定义函数,利用map去做 map(函数, 序列) 将序列的各项经过函数处理, 然后返回到一个新列表中. #itertools. ...
- [Javascript] Chaining the Array map and filter methods
Both map and filter do not modify the array. Instead they return a new array of the results. Because ...
随机推荐
- react native Expo适配全面屏/Expo识别全面屏和正常屏
一.最新版本的expo已经默认支持了全面屏,即不会像react native cli一样出现底部黑边 二.但是全面屏通过Dimensions.get('window')获取的高度还是不准确,因为全面屏 ...
- 16Flutter中的路由 基本路由 基本路由跳转传值(上)
/* Flutter中的普通路由.普通路由传值.命名路由.命名路由传值 Flutter中的路由通俗的讲就是页面跳转.在Flutter中通过Navigator组件管理路由导航. 并提供了管理堆栈的方法. ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_01-自定义查询页面-服务端-Dao
在页面输入查询条件,查询符合条件的页面信息. 查询条件如下: 站点Id:精确匹配 模板Id:精确匹配 页面别名:模糊匹配 spring mongoDB如何自定义条件 在Repository的findA ...
- Linux命令之iptables
从CentOS7开始,系统自带的防火墙更改为firewalld,但同样支持iptables,不过只有iptables命令,如果想要使用iptables服务需要自行安装iptables-server. ...
- expect实现免交互
如果想写一个能够自动处理输入输出的脚本又不想面对C或Perl,那么expect是最好的选择.它可以用来做一些Linux下无法做到交互的一些命令操作. (1).安装和使用expect expect是不会 ...
- 【leetcode_easy】543. Diameter of Binary Tree
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
- jquery+flask+keras+nsfw快速搭建一个简易鉴黄工具
1. demo 地址:http://www.huchengchun.com:8127/porn_classification 接口说明: 1. http://www.huchengchun.com:8 ...
- Vue Cli3.0 使用jquery
参考链接:https://blog.csdn.net/ai520587/article/details/84098601
- redis设置密码和其它服务器连接
在cenos中 vim /etc/redis.conf 中 /输入 requirepass enter件一下 小写 n 一下 吧 # requirepass #去掉,后面写你的密码 #其它机器连接 v ...
- Mac/Windows 跳过sourcetree的注册环节
前几天在电脑上装了个sourcetree,结果它硬要我注册,烦得很. 于是查了一下怎么跳过注册环节,结果还真有,试了一下,真给力! 特此记录. MAC版本: 打开sourcetree 关闭 ...