var activeEmployee = _.filter(employees, function (employee) {
return _.some(employee.Employee.EmployeeRoles, { 'Employee_Role_Id': 1 });
})
如何使用_.filter过滤以下条件
1.Employee_Role_Id 1或3
2.Active_Ind as true
{{1}}
答案 0 :(得分:1)
你可以这样做:
var employees = [{"Employee_Id": 252156,"Active_Ind": true,"EmployeeRoles": [{"Employee_Role_Id": 1,"Employee_Role_Nm": "Tax Prep"}]},{"Employee_Id": 252156,"Active_Ind": true,"EmployeeRoles": [{"Employee_Role_Id": 1,"Employee_Role_Nm": "Tax Prep"},{"Employee_Role_Id": 2,"Employee_Role_Nm": "Tax Prep"}]},{"Employee_Id": 252156,"Active_Ind": true,"EmployeeRoles": [{"Employee_Role_Id": 1,"Employee_Role_Nm": "Tax Prep"},{"Employee_Role_Id": 3,"Employee_Role_Nm": "Tax Prep"}]},{"Employee_Id": 252156,"Active_Ind": false,"EmployeeRoles": [{"Employee_Role_Id": 1,"Employee_Role_Nm": "Tax Prep"},{"Employee_Role_Id": 2,"Employee_Role_Nm": "Tax Prep"}]}],
activeEmployee = _.filter(employees, function (employee) {
return employee.Active_Ind && _.some(employee.EmployeeRoles, function(role) {
return 1 === role.Employee_Role_Id || 3 === role.Employee_Role_Id;
});
});
console.log(activeEmployee);

.as-console-wrapper { max-height: 100% !important; top: 0; }

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
答案 1 :(得分:0)
您可以将所有some
值与您想要的值相交,而不是Employee_Role_Id
:
var activeEmployee = _.filter(employees, e=>e.Active_Ind
&& _.intersection([1,3],_.pluck(e.EmployeeRoles,'Employee_Role_Id')).length);
let employees = [
{
"Employee_Id": 252156,
"Active_Ind": true,
"EmployeeRoles": [
{
"Employee_Role_Id": 1,
"Employee_Role_Nm": "Tax Prep"
}
]
},
{
"Employee_Id": 252156,
"Active_Ind": true,
"EmployeeRoles": [
{
"Employee_Role_Id": 1,
"Employee_Role_Nm": "Tax Prep"
},
{
"Employee_Role_Id": 2,
"Employee_Role_Nm": "Tax Prep"
}
]
},
{
"Employee_Id": 252156,
"Active_Ind": true,
"EmployeeRoles": [
{
"Employee_Role_Id": 1,
"Employee_Role_Nm": "Tax Prep"
},
{
"Employee_Role_Id": 3,
"Employee_Role_Nm": "Tax Prep"
}
]
},
{
"Employee_Id": 252156,
"Active_Ind": false,
"EmployeeRoles": [
{
"Employee_Role_Id": 1,
"Employee_Role_Nm": "Tax Prep"
},
{
"Employee_Role_Id": 2,
"Employee_Role_Nm": "Tax Prep"
}
]
}
];
var activeEmployee = _.filter(employees, e=>e.Active_Ind && _.intersection([1,3],_.pluck(e.EmployeeRoles,'Employee_Role_Id')).length);
console.log(activeEmployee);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;