gpt4 book ai didi

javascript - 无法为访问函数创建 javaScript 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:20:38 24 4
gpt4 key购买 nike

我的应用程序中有一些选项卡,例如报告、任务等...用户也有不同的权限,如 reports.add、tasks.delete。我需要创建函数来检查允许用户做什么。

// for example array with all current user permissions
// this permissions mean user is allowed to do everything with tasks
// add and edit reports, but not allowed to to delete it

const permissions = ['reports.add', 'reports.edit', 'tasks'];

const isAllowed = (condition) => {
return permissions.some((permission) => {
// here is problem, I can't create algorithm
});
};


// When user clicks delete report button
// I expect to use this function like this

if (isAllowed('reports.delete')) {
deleteReport()
}

最佳答案

如果 conditionpermission 开头,您可以在 permissions 中搜索。

const
permissions = ['reports.add', 'reports.edit', 'tasks'],
isAllowed = condition => permissions.some(permission => condition.startsWith(permission));

console.log(isAllowed('reports.add')); // true
console.log(isAllowed('tasks.edit')); // true
console.log(isAllowed('tasks')); // true
console.log(isAllowed('task')); // false

关于javascript - 无法为访问函数创建 javaScript 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58105253/

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