es6 filter() 数组过滤方法总结
程序员文章站
2022-05-27 09:14:02
array.every(x=>x)是每一个都要满足
array.some(x=>x)是有一个满足。
array.find(findindex),返回符合条件...
array.every(x=>x)是每一个都要满足
array.some(x=>x)是有一个满足。
array.find(findindex),返回符合条件的第一个值。
array.filter(过滤成新的数组)
数组的方法分为两类
1)改变原数组
push,pop,shift,unshift,sort,reverse,splice
2)不改变原数组concat,join-->
split,tostringpush:从数组最后一位开始加数据
pop:把数组最后一位剪切
shift:在数组最前一位剪切
unshift:在数组最前一位加数
reverse:把原数组逆转
splice:arr.splice(从第几位开始,截取多少长度,在切口处添加新数据)
concat :连接join:返回字符串
slice:截取arr.slice(从该为开始截取,截取到该为)
示例
1.创建一个数组,判断数组中是否存在某个值
var newarr = [ { num: 1, val: 'ceshi', flag: 'aa' }, { num: 2, val: 'ceshi2', flag: 'aa2' } ] console.log(newarr.filter(item => item.num===2 ))
2.也可以通过上面方法过滤掉num为2的留下num为1的
var newarr = [ { num: 1, val: 'ceshi', flag: 'aa' }, { num: 2, val: 'ceshi2', flag: 'aa2' } ] console.log(newarr.filter(item => item.num!=2 ))
3.去掉空数组空字符串、undefined、null
var arr = ['1','2',undefined, '3.jpg',undefined] var newarr = arr.filter(item => item) console.log(newarr) var arr = ['1','2',null, '3.jpg',null] var newarr = arr.filter(item => item) console.log(newarr) >//空字符串里面不能包含空格 var arr = ['1','2','', '3.jpg',''] var newarr = arr.filter(item => item) console.log(newarr)
4.去掉数组中不符合项
var arr = [20,30,50, 96,50] var newarr = arr.filter(item => item>40) console.log(newarr)
5.过滤不符合项
var arr = ['10','12','23','44','42'] var newarr = arr.filter(item => item.indexof('2')<0) console.log(newarr)
6.数组去重
var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2]; var arr2 = arr.filter((x, index,self)=>self.indexof(x)===index) console.log(arr2); //[1, 2, 3, 4, 5, 6, 7, 8, 0, 56]
7
/* 有一个对象数组 a ,将a数中对象某个属性的值存储到b数组中 */ var porducts = [ {name:"cucumber",type:"vegetable"}, {name:"banana",type:"fruit"}, {name:"celery",type:"vegetable"}, {name:"orange",type:"fruit"}, ]; // es5 var filteredproducts = []; for(var i = 0;i < porducts.length; i ++){ if(porducts[i].type === "fruit"){ // 如果条件满足就把当前的值推入 filteredproducts.push(porducts[i]) } } // console.log(filteredproducts)//0: {name: "banana", type: "fruit"}1: {name: "orange", type: "fruit"}length: 2__proto__: array(0) // es6 var filter2 = porducts.filter(function(porduct){//对porducts数组对象过滤如果porduct.type === "fruit"就return出去,再用一个变量接住 return porduct.type === "fruit" }) console.log(filter2)
8
/* 需求二 有一个对象数组a,过滤掉不满足以下条件对象 条件:蔬菜 数量大于0 价格小于10 */ var products = [ {name:"cucumber",type:"vegetable",quantity:0,price:1}, {name:"banana",type:"fruit",quantity:10,price:16}, {name:"celery",type:"vegetable",quantity:30,price:8}, {name:"orange",type:"fruit",quantity:3,price:6}, ]; products = products.filter(function(product){ return product.type === "vegetable" && product.quantity > 0 && product.price < 10 }) console.log(products)//0: {name: "celery", type: "vegetable", quantity: 30, price: 8}name: "celery"price: 8quantity: 30type: "vegetable"__proto__: objectlength: 1__proto__: array(0)
9
/* 需求三: 有两个数组a,b,根据a中的id值 ,过滤掉b数组不符合的数据 */ var post = {id:4,title:"javascript"}; var comments = [ {postid:4,content:'angular4'}, {postid:2,content:'vue.js'}, {postid:3,content:'node.js'}, {postid:4,content:'react.js'}, ]; function commentsforpost(post,comments){ return comments.filter(function(comment){ return comment.postid === post.id; }) } console.log(commentsforpost(post,comments)) // 0: {postid: 4, content: "angular4"}1: {postid: 4, content: "react.js"}length: 2__proto__: array(0)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: IOS中实现图片点击全屏预览
下一篇: ps怎么给树叶添加毛毛雨点效果?