我有这样的数组
roles = ['AnyRulesGo@admin', 'NoRules@admin', 'HowYesNo@history', 'MamaMia@survey'];
我有这样的东西
externalLinks = [
{
link: '#',
icon: 'Icon',
translation: 'Home',
role: '@history',
active: false,
visible: false
},
{
link: '#',
icon: 'Task',
translation: 'Tasks',
role: '@task',
active: false,
visible: false
},
{
link: '#',
icon: 'Files',
translation: 'Files',
role: '@admin',
active: true,
visible: false
}
];
我需要一些函数来检查externaLinks的值角色是否存在于数组角色中,并从更新外部链接中的该值 visible > false 到 true
我没有更多的代码,因为我什至不知道从哪里开始,任何帮助都会很大,谢谢
问题之一是我确实只从@开始才开始使用netire角色名称,这意味着我需要剪切该字符串,然后进行比较?
我已经尝试过使用此功能,但是没有运气
function objectsAreSame(x, y) {
var objectsAreSame = true;
for(var propertyName in x) {
if(x[propertyName] !== y[propertyName]) {
objectsAreSame = false;
break;
}
}
return objectsAreSame;
}
答案 0 :(得分:2)
您可以使用externalLinks
遍历array#forEach
中的每个对象。然后使用roles
和array#some
检查string#incldues
数组中的角色,并更新visible
键的值。
let roles = ['AnyRulesGo@admin', 'NoRules@admin', 'HowYesNo@history', 'MamaMia@survey'],
externalLinks = [ { link: '#', icon: 'Icon', translation: 'Home', role: '@history', active: false, visible: false }, { link: '#', icon: 'Task', translation: 'Tasks', role: '@task', active: false, visible: false }, { link: '#', icon: 'Files', translation: 'Files', role: '@admin', active: true, visible: false } ];
externalLinks.forEach(o => {
o.visible = false;
if(roles.some(r => r.includes(o.role)))
o.visible = true;
});
console.log(externalLinks);
答案 1 :(得分:0)
您可以使用forEach
循环访问外部链接。然后使用some
和includes
检查角色是否存在。
roles = ['AnyRulesGo@admin', 'NoRules@admin', 'HowYesNo@history', 'MamaMia@survey'];
externalLinks.forEach((extLink) => {
if(roles.some(role => role.includes(extLink.role))){
extLink.visible = true;
}
})
答案 2 :(得分:-1)
进行两个循环:
//Make all false
externalLinks.forEach(x=>x.visible=false);
//for each role, filter by role and forEach value filtered, make visible
role.forEach(role=>{
let busc="@"+role.split('@')[1]
externalLinks.filter(x=>x.role==busc)
.forEach(xfilter=>xfilter.visible=true)
})
//If only want a role "roleSearch"
let busc="@"+roleSearch.split('@')[1]
externalLinks.filter(x=>x.role==busc)
.forEach(xfilter=>xfilter.visible=true)