Javascript 筛选多个属性和多个值上的嵌套数组

Javascript 筛选多个属性和多个值上的嵌套数组,javascript,arrays,Javascript,Arrays,我想在JS数组中对用户公司角色对象创建灵活的表过滤。用户应能够在匹配时使用AND(&&&)操作数对多个具有多个值的对象属性进行过滤 我希望您能给我一些关于如何继续实现这里的逻辑的提示。当然,我可以在数组中循环并使用多个嵌套的if语句,但也许有更干净、更漂亮的方法 用户公司角色数组 const userArray = [ { "id": "peterpan", "name": "Peter pan", "company": [ { "

我想在JS数组中对用户公司角色对象创建灵活的表过滤。用户应能够在匹配时使用AND(&&&)操作数对多个具有多个值的对象属性进行过滤

我希望您能给我一些关于如何继续实现这里的逻辑的提示。当然,我可以在数组中循环并使用多个嵌套的if语句,但也许有更干净、更漂亮的方法

用户公司角色数组

const userArray = [
{
    "id": "peterpan",
    "name": "Peter pan",
    "company": [
        {
            "id": "12345678",
            "name": "Company A",
            "roles": [
                "Role A",
                "Role B",
                "Role C"
            ]
        }
    ],
    "systemRoles": [
        {
            "systemName": "System A",
            "role": "Admin"
        },
        {
            "systemName": "System B",
            "role": "User"
        }
    ]
},
{
    "id": "robinhood",
    "name": "Robin Hood",
    "company": [
        {
            "id": "9876543",
            "name": "Company B",
            "roles": [
                "Role A"
            ]
        },
        {
            "id": "546372",
            "name": "Company C",
            "roles": [
                "Role A"
            ]
        }
    ],
    "systemRoles": [
        {
            "systemName": "System B",
            "role": "User"
        }
    ]
},
{
    "id": "biggiant",
    "name": "Big Giant",
    "company": [
        {
            "id": "546372",
            "name": "Company C",
            "roles": [
                "Role A"
            ]
        }
    ],
    "systemRoles": [
        {
            "systemName": "System B",
            "role": "User"
        }
    ]
}
];
筛选对象

const filter = {
        includeUserIds: [], // filters 'user.id' that matches values in this array
        includeCompanyNames: [], // filters 'user.company.name' that matches values in this array
        includeSystemRoleNames: [], // filters 'user.systemRoles.role' that matches values in this array
        includeCompanyRoles: [], // filters 'user.company.roles' that matches values in this array
        excludeSystemRoleNames: [], // filters 'user.systemRoles.role' that **DOES NOT** match values in this array
        excludeCompanyRoles: [] // filters 'user.company.roles' that **DOES NOT** match values in this array
    }
匹配 过滤数组时,我希望过滤器与用户对象匹配,如下所示(伪代码):

示例1:按用户id筛选用户:

const filter = {
        includeUserIds: ['peterpan', 'robinhood'],
        includeCompanyNames: [],
        includeSystemRoleNames: [], 
        includeCompanyRoles: [], 
        excludeSystemRoleNames: [],
        excludeCompanyRoles: []
    }
将与Peter Pan和Robin Hood用户一起返回阵列

示例2:按公司名称筛选

const filter = {
        includeUserIds: [],
        includeCompanyNames: ['Company C'],
        includeSystemRoleNames: [], 
        includeCompanyRoles: [], 
        excludeSystemRoleNames: [],
        excludeCompanyRoles: []
    }
将与罗宾汉和大巨人用户一起返回阵列

示例3:按公司角色和系统角色筛选

const filter = {
        includeUserIds: [],
        includeCompanyNames: [],
        includeSystemRoleNames: ['User'], 
        includeCompanyRoles: ['Role A'], 
        excludeSystemRoleNames: ['Admin'],
        excludeCompanyRoles: []
    }

将返回Robin Hood和Big Giant用户的数组

我将为每种类型的筛选器创建一个决策函数,并将其放入具有与
筛选器
相同属性的对象中。每个函数都应该接受两个参数:
item
类型为
any
filter
类型为
string[]
。所以它看起来像:

const deciders = {
    includeUserIds: (item, filter) => { ... },
    includeCompanyNames: (item, filter) => { ... },
    includeSystemRoleNames: (item, filter) => { ... },
    includeCompanyRoles: (item, filter) => { ... },
    excludeSystemRoleNames: (item, filter) => { ... },
    excludeCompanyRoles: (item, filter) => { ... },
};
然后过滤是原始数组的一个简单的
过滤器,如:

const filtered = userArray.filter(user => {
  let include = true;
  Object.keys(deciders).forEach(type => include &= deciders[type](user, filter[type]));
  return include;
});

谢谢你能给出一个在一个deciders函数中逻辑的例子吗?基本上它是简单的
(项目,过滤器)=>过滤器。indexOf(项目)!=-1
但可能有所不同,例如,在
公司
的情况下,对象的实际价值更深
const filtered = userArray.filter(user => {
  let include = true;
  Object.keys(deciders).forEach(type => include &= deciders[type](user, filter[type]));
  return include;
});