es6filter()数组过滤⽅法总结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]
es6字符串转数组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数组对象过滤如果pe === "fruit"就return出去,再⽤⼀个变量接住
pe === "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){
pe === "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)
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论