其他分享
首页 > 其他分享> > 筛选(1)

筛选(1)

作者:互联网

A数组:

var aa = [
    {name:'Anne', age: 23, gender:'female'},
    {name:'Leila', age: 16, gender:'female'},
    {name:'Jay', age: 19, gender:'male'},
    {name:'Mark', age: 40, gender:'male'}
]

一、筛选出Jay的资料:  用filter方法,判断name是否为目标名字即可

console.log(aa.filter(item=>item.name==='Jay'));
//[{…}]
//0: {name: "Jay", age: 19, gender: "male"}
//length: 1
//__proto__: Array(0)

二、筛选出Jay、Mark的资料:用for循环遍历目标数组,然后用find方法找到后push到结果数组里

 let bb=['Jay','Mark']
 let result = []
    for(let i = 0; i < bb.length; i++) {

        result.push(aa.find(item => item.name = bb[i]))
    }

//输出为 [{name:'Anne', age: 23, gender:'female'},{name:'Jay', age: 19, gender:'male'}]

三、筛选出Jay或者23岁的资料:用filter方法,判断条件之间是或的关系。

aa.filter(item => item.name == 'Jay' || item.age == 23)

 

标签:aa,name,Jay,gender,age,item,筛选
来源: https://www.cnblogs.com/a1-top/p/14062807.html