我有此数据:
roles = [
{roleId: "69801", role: "ADMIN"}
{roleId: "69806", role: "SUPER_ADMIN"}
{roleId: "69805", role: "RB"}
{roleId: "69804", role: "PILOTE"}
{roleId: "69808", role: "VENDEUR"}
{roleId: "69807", role: "SUPER_RB"}
]
我必须过滤我的表以检查是否存在包含指定 Angular 色值的对象。
我的功能应如下所示:
checkRoleExistence(role){
// if role exists on one of the objects return true
// else returne false
}
要使用它,我会像这样:
let ifExists = this.checkRoleExistence("PILOTE") ;
我想使用 Ecmascript 的“过滤器” 函数。
建议?
最佳答案
您可以使用some
方法和解构。
let roles = [ {roleId: "69801", role: "ADMIN"}, {roleId: "69806", role: "SUPER_ADMIN"}, {roleId: "69805", role: "RB"}, {roleId: "69804", role: "PILOTE"}, {roleId: "69808", role: "VENDEUR"}, {roleId: "69807", role: "SUPER_RB"} ]
const checkRoleExistence = roleParam => roles.some( ({role}) => role == roleParam)
console.log(checkRoleExistence("ADMIN"));
console.log(checkRoleExistence("RA"));
console.log(checkRoleExistence("RB"));