【发布时间】:2018-06-26 06:57:09
【问题描述】:
我一定是在想这个问题的解决方案,但似乎无法做到这一点。
我有一个数组object,如下所示:
[
{ ItemID: 1, Path: '/Admin', Name: 'Admin' },
{ ItemID: 2, Path: '/Product', Name: 'Product' },
{ ItemID: 1, Path: '/Reports', Name: 'Reports' }
]
我想映射每个项目,并且我需要为每个项目运行一个函数,该函数将返回它们是否具有访问权限。即布尔值(是/否)。
到目前为止,我有这样的事情:
const newData = data.map((curr, val , arr) => {
if (checkAccess(username, curr.Name )) { //checkAccess returns true or false
return { ...current };
}
});
我只想退回他们有权访问的那些。
所以假设用户无法访问Admin,最终的对象应该是:
[
{ ItemID: 2, Path: '/Product', Name: 'Product' },
{ ItemID: 1, Path: '/Reports', Name: 'Reports' }
]
编辑:
问题还在于该函数没有返回真/假
function checkInGroup(username, name) {
let inGroup = "";
ad.isUserMemberOf(username, name, function(err, isMember) {
if (err) {
return res.json("Error ", err);
}
inGroup = isMember; //this part returns true
});
return inGroup; //this seems to return empty string
}
【问题讨论】:
-
你需要使用.filter而不是.map
标签: javascript function ecmascript-6