如何检查对象键是否在按数组键过滤的数组中?

时间:2018-11-16 08:27:05

标签: javascript typescript

我正在使用Angular 2 Ionic应用程序-

有没有一种方法可以按我的personArr中的年龄过滤所有人,然后查找我的personObj中的任何键是否匹配personArr名称并返回布尔值?

我有一个示例数组,例如:

personsArr = [{
        "name": "Ram",
        "email": "ram@gmail.com",
        "age": 23
    },
    {
        "name": "peter",
        "email": "shyam23@gmail.com",
        "age": 23
    },
    {
        "name": "John",
        "email": "john@gmail.com",
        "age": 33
    },
    {
        "name": "Bob",
        "email": "bob32@gmail.com",
        "age": 41
    }
]

和这样的对象

personObj = {
    "peter": "helper",
    "Ram": "helper",
    "Bob": "helper"
}

如何按年龄过滤personArr,然后检查personObj中的任何键是否与人名匹配?

可以肯定,我可以通过循环执行此操作,但是有更好的方法吗?

感谢任何帮助或建议。

5 个答案:

答案 0 :(得分:3)

您可以尝试使用过滤器和一些过滤器。您可以根据条件进行过滤,然后检查对象是否匹配。希望这会有所帮助。

const personsArr = [{
    "name": "Ram",
    "email": "ram@gmail.com",
    "age": 23
    }, {
        "name": "peter",
        "email": "shyam23@gmail.com",
        "age": 23
    }, {
        "name": "John",
        "email": "john@gmail.com",
        "age": 33
    }, {
        "name": "Bob",
        "email": "bob32@gmail.com",
        "age": 41
    }];

const personObj = { "Ram": "helper", "Bob": "helper" };

const personObj2 = { "Ram2": "helper", "Bob2": "helper" };

const checkArr = (age, arr, obj) => arr.filter((f) => f.age > age).some((d) => obj[d.name]);

console.log('Should be true');
console.log(checkArr(30, personsArr, personObj));
console.log('Should be false');
console.log(checkArr(30, personsArr, personObj2));

答案 1 :(得分:2)

您在这里

// Filter by age
const legalAge = 18 // for example
const boolean = personsArr
    // Actual filtering 
    .filter(p => p.age > legalAge)
    // Check if there is any `name` in personsArr matching any key in personObj
    .some(p =>
        Object.keys(personObj).some(pk => pk === p.name)
    )

// Sort by age (maybe you meant this instead of filtering)
personsArr.sort((a, b) => a.age > b.age ? 1 : -1)
const boolean2 = personsArr.some(p =>
    Object.keys(personObj).some(pk => pk === p.name)
)

答案 2 :(得分:1)

如果我对您的理解正确,那么您需要这样的东西:

const isFound = personsArr.some((item) => item.age > 30 && personObj.includes(item.name));

当然item.age > 30应该替换为您的过滤条件。

答案 3 :(得分:1)

无论您是否循环播放,都涉及循环。最简单的答案可能是使用Array.filter

personsArr.filter((val) => val.age === age && Object.keys(roles).indexOf(val.name) > -1);

这是一个完整的示例:

const personsArr = [
    { "name": "Ram", "email": "ram@gmail.com", "age": 23 },
    { "name": "peter", "email": "shyam23@gmail.com", "age": 23 },
    { "name": "John", "email": "john@gmail.com", "age": 33 },
    { "name": "Bob", "email": "bob32@gmail.com", "age": 41 }
];

const personObj = { "peter": "helper", "Ram": "helper", "Bob": "helper" };

function filter(age: number, roles: { [name: string]: string }) {
    const keys = Object.keys(roles);
    return personsArr.filter((val) => val.age === age && keys.indexOf(val.name) > -1);
}

// 0: Object { name: "Ram", email: "ram@gmail.com", age: 23 }
​// 1: Object { name: "peter", email: "shyam23@gmail.com", age: 23 }
console.log(filter(23, personObj));

// 0: Object { name: "Bob", email: "bob32@gmail.com", age: 41 }
console.log(filter(41, personObj));

答案 4 :(得分:1)

var personsArr = [    
{"name":"Ram", "email":"ram@gmail.com", "age":23},    
{"name":"peter", "email":"shyam23@gmail.com", "age":23},  
{"name":"John", "email":"john@gmail.com", "age":33},    
{"name":"Bob", "email":"bob32@gmail.com", "age":41}   
];

var personObj = {"peter":"helper","Ram":"helper", "Bob":"helper"}

var newPersonsArr = personsArr.filter((person) => person.age<30);
var check = false;
for(var key in personObj) {
  newPersonsArr.map((person) => {
  if(person.name === key){
   check = true
  }
  });
}


console.log(check);