gpt4 book ai didi

javascript - 如果找到具有正确属性的obj,则js返回true

转载 作者:行者123 更新时间:2023-11-30 13:53:02 24 4
gpt4 key购买 nike

let userScopes = [
{
scope: 'user',
actions: ['create', 'read', 'update'],
},
];

function checked(scope, actions) {
return !!~userScopes.find(
_scope =>
_scope.scope === scope && _scope.actions.lastIndexOf(actions) !== -1
);
}

console.log(checked("user","create"));//true
console.log(checked("users","create"));//false
console.log(checked("user","creat"));//false
console.log(checked("make","create"));//false

我必须确保如果找到具有以下属性的对象,则它返回 true,否则返回 false。

但是它行不通,有什么建议吗?

最佳答案

您可以使用 Array#some 查看和 Array#includes .

function checked(scope, action) {
return userScopes.some(o => o.scope === scope && o.actions.includes(action));
}

let userScopes = [{ scope: 'user', actions: ['create', 'read', 'update'] }];

console.log(checked("user", "create")); // true
console.log(checked("users", "create")); // false
console.log(checked("user", "creat")); // false
console.log(checked("make", "create")); // false

关于javascript - 如果找到具有正确属性的obj,则js返回true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57850743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com