我的应用程序中有一些选项卡,如报告、任务等......此外,用户具有不同的权限,如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 itconst 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()}
2 回答
![?](http://img1.sycdn.imooc.com/5458502c00012d4a02200220-100-100.jpg)
蛊毒传说
TA贡献1895条经验 获得超3个赞
permissions如果condition以permission.开头,您可以搜索。
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
![?](http://img1.sycdn.imooc.com/5458655200013d9802200220-100-100.jpg)
慕慕森
TA贡献1856条经验 获得超17个赞
您可以只创建一个普通函数并避免在另一个函数中使用匿名函数...
function isAllowed(permission) {
return condition; //or some if-else
}
添加回答
举报
0/150
提交
取消