JavaScript

超轻量级php框架startmvc

es6 filter() 数组过滤方法总结

更新时间:2020-08-19 22:36:01 作者:startmvc
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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

es6 filter() 数组过滤 es6过滤filter