es6 filter方法的使用
程序员文章站
2022-03-21 11:37:37
var obj = [{id:1,name:'张三',beizhu:'123asd'},{id:2,name:'李四',beizhu:'122fghasd'},{id:3,name:'王五',beizhu:'12df4sd'},]//取出数组对应的值console.log(obj.filter(item =>(item.id==1))).....
var obj = [
{
id:1,
name:'张三',
beizhu:'123asd'
},
{
id:2,
name:'李四',
beizhu:'122fghasd'
},
{
id:3,
name:'王五',
beizhu:'12df4sd'
},
]
//取出数组对应的值
console.log(obj.filter(item =>(item.id==1)))
//{id: 1, name: "张三", beizhu: "123asd"}
//过滤数组中undefined,null,空字符串
var arr1 = [1,'',undefined,null,'aaa']
console.log(arr1.filter(item=>(item)))
//[1, "aaa"]
//去除数组中不符合的值
var arr2 = [2,40,5,100,8,90]
console.log(arr2.filter(item=>(item>40)))
//[100, 90]
//去掉数组中不符合项
var arr3= [22,40,5,200,8,90]
console.log(arr3.filter(item=> item.toString().indexOf(2)<0))
//[40, 5, 8, 90]
// //数组去重
var arr4 = [11,22,33,44,44,22,55]
console.log(arr4.filter((num,index,list)=>list.indexOf(num) == index))
// [11, 22, 33, 44, 55]
本文地址:https://blog.csdn.net/weixin_40808668/article/details/109643324