通过Javascript中的角色数组过滤用户对象数组

时间:2018-09-08 11:16:15

标签: javascript arrays filter ecmascript-6 find

我有一组用户对象。 我想根据用户角色数组对其进行过滤。

filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']

const users = [{
		"id": 1,
		"email": "user1@test.com",
		"name": "User1",
		"roles": [{
				"id": 1,
				"code": "ROLE_ADMINISTRATOR",
				"name": "Administrator"
			},
			{
				"id": 2,
				"code": "ROLE_SELLER",
				"name": "Seller"

			}
		]
	}, {
		"id": 2,
		"email": "user2@test.com",
		"name": "User2",
		"roles": [{
			"id": 1,
			"code": "ROLE_ADMINISTRATOR",
			"name": "Administrator"
		}]
	}, {
		"id": 3,
		"email": "user3@test.com",
		"name": "User3",
		"roles": [{
			"id": 1,
			"code": "ROLE_ADMINISTRATOR",
			"name": "Administrator"
		}]
	}, {
		"id": 4,
		"email": "user4@test.com",
		"name": "User4",
		"roles": [{
				"id": 1,
				"code": "ROLE_ADMINISTRATOR",
				"name": "Administrator"
			},
			{
				"id": 2,
				"code": "ROLE_SELLER",
				"name": "Seller"

			}
		]
	}, {
		"id": 5,
		"email": "user5@test.com",
		"name": "User5",
		"roles": [{
			"id": 5,
			"code": "ROLE_LAWYER",
			"name": "Lawyer"

		}]
	}, {
		"id": 6,
		"email": "user6@test.com",
		"name": "User6",
		"roles": [{
			"id": 2,
			"code": "ROLE_SELLER",
			"name": "Seller"

		}]
	},
	{
		"id": 7,
		"email": "user7@test.com",
		"name": "User7",
		"roles": [{
			"id": 9,
			"code": "ROLE_SLA",
			"name": "sla"
		}]
	},
	{
		"id": 8,
		"email": "user8@test.com",
		"name": "User8",
		"roles": [{
			"id": 8,
			"code": "ROLE_BANK",
			"name": "Bank"

		}]
	},
	{
		"id": 9,
		"email": "user9@test.com",
		"name": "User9",
		"roles": [{
			"id": 7,
			"code": "ROLE_CPF",
			"name": "CPF"
		}]
	}
]
const filter = ['ROLE_SELLER', 'ROLE_BANK', 'ROLE_CPF', 'ROLE_SLA', 'ROLE_LDAU']

const filteredUsers = users.filter(user => !user.roles.find(role => filter.includes(role.id)))

console.log(filteredUsers)

预期结果

 [{
        "id": 1,
        "email": "user1@test.com",
        "name": "User1",
        "roles": [{
                "id": 1,
                "code": "ROLE_ADMINISTRATOR",
                "name": "Administrator"
            },
            {
                "id": 2,
                "code": "ROLE_SELLER",
                "name": "Seller"

            }
        ]
    }, {
        "id": 4,
        "email": "user4@test.com",
        "name": "User4",
        "roles": [{
                "id": 1,
                "code": "ROLE_ADMINISTRATOR",
                "name": "Administrator"
            },
            {
                "id": 2,
                "code": "ROLE_SELLER",
                "name": "Seller"

            }
        ]
    }, {
        "id": 6,
        "email": "user6@test.com",
        "name": "User6",
        "roles": [{
            "id": 2,
            "code": "ROLE_SELLER",
            "name": "Seller"

        }]
    },
    {
        "id": 7,
        "email": "user7@test.com",
        "name": "User7",
        "roles": [{
            "id": 9,
            "code": "ROLE_SLA",
            "name": "sla"
        }]
    },
    {
        "id": 8,
        "email": "user8@test.com",
        "name": "User8",
        "roles": [{
            "id": 8,
            "code": "ROLE_BANK",
            "name": "Bank"

        }]
    },
    {
        "id": 9,
        "email": "user9@test.com",
        "name": "User9",
        "roles": [{
            "id": 7,
            "code": "ROLE_CPF",
            "name": "CPF"
        }]
    }
 ]

2 个答案:

答案 0 :(得分:1)

我正在尝试成像您想要接收的结果。...

您想按具有可能角色的数组来筛选用户,如果用户具有要将其角色传递给'filteredUsers'数组的筛选器数组中的一个角色,则另一个世界吗?

filter.includes(role.id)-我想这是错误的,可能是您想要    通过role.code过滤?

Array.find()不支持资源管理器 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Array.includes()-也不支持资源管理器。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

答案 1 :(得分:1)

我认为发生这种情况的原因是您试图将角色ID与包含角色代码的“过滤器”数组进行匹配。根据我从预期答案中观察到的结果,您正在寻找具有任何一种角色都适合该过滤器的用户。因此,以下内容将按角色代码进行过滤并获得预期结果

const filteredUsers = users.filter((user) => {
    return user.roles.map(role=>filter.includes(role.code)).includes(true)
})

这行代码基本上将过滤器映射到每个用户的每个角色对象,如果他们的角色代码包含在过滤器中,它将为数组(返回map()的数组)添加true,并且为filter函数添加true map()返回数组先包含true,然后包含true(因此,整个数组基本上是||)