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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按Javascript中的角色数组过滤用户对象的数组相关的知识,希望对你有一定的参考价值。

我有一组用户对象。我想根据用户角色数组过滤它们。

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"
        }]
    }
 ]
答案

我试图想象你想要收到什么结果....

您希望按可能角色的数组过滤用户,如果用户具有要将其传递给'filteredUsers'数组的过滤器数组的角色之一,则需要另一个世界?

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

Array.find()不支持Explorer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

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

另一答案

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

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

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

以上是关于按Javascript中的角色数组过滤用户对象的数组的主要内容,如果未能解决你的问题,请参考以下文章

如何在JavaScript中过滤嵌套数组中的对象?

在Javascript中按多个条件过滤数组

根据 GPS 位置之间的距离过滤对象数组(javascript)

基于javascript中的另一个数组过滤对象数组

如何使用Javascript中的数组值过滤对象?

动态过滤嵌套javascript对象数组中的数据[重复]