ES6中filter过滤器的⽤法
var sexData=["男","⼥","⼥","男","⼥"];
var filter2=sexData.filter(function(sex){
return sex==="⼥"
})
//console.log(filter2) ["⼥", "⼥", "⼥"]
var porducts =[
{name:'apple',type:'red'},
{name:'orange',type:'orange'},
{name:'banana',type:'yellow'},
{name:'mango',type:'yellow'}
]
;
var filter2=porducts.filter(function(item){
pe==='yellow'
})
//console.log(filter2)
//0: {name: "banana", type: "yellow"}1: {name: "mango", type: "yellow"}
1. filter()
返回符合条件的元素的数组[]
筛选回调函数,有三个参数
语法:
array.filter((value, index, arr) => {value === '匹配对象'})
特殊⽤法:
1. 去掉空字符串、undefined、null
array.filter((value, index, arr) => {value})
2. 数组去重
array.filter((value, index, arr) => {arr.indexOf(value) === index})
//1. 去掉空字符串、undefined、null
var porducts =[
{name:''},
{name:"哈哈"}
];
var filter2=porducts.filter(function(item){
return item.name
})
//console.log(filter2)
//打印得出 0: {name: "哈哈"}
//2. 数组去重
array.filter((value, index, arr)=>{arr.indexOf(value)=== index})
var porducts =['苹果','⾹蕉','苹果','芒果']
filter过滤对象数组var filter2=porducts.filter(function(item,index,porducts){
return porducts.indexOf(item)==index
})
//console.log(filter2)
/
/ ["苹果", "⾹蕉", "芒果"]
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论