[英]Check if value is in array and return true or false angular 6+
I have some array like this我有一些这样的数组
roles = ['AnyRulesGo@admin', 'NoRules@admin', 'HowYesNo@history', 'MamaMia@survey'];
And i have some object like this我有一些这样的对象
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
}
];
I need some function to check does value role in externaLinks exists in array roles , and update that value visible in externalLinks from false to true我需要一些功能来检查确实在externaLinks价值作用,在阵列的角色存在,并更新该值在externalLinks可见从假到真
I dont have much more code, because i dont know even where to start from, any help will be great, thanks我没有更多的代码,因为我什至不知道从哪里开始,任何帮助都会很棒,谢谢
One of the problem is that i done have netire role name only started from @ it means I need to cut that string, and than compare?问题之一是我完成了仅从@ 开始的角色名称,这意味着我需要剪切该字符串,然后进行比较?
I have tried with this function, but no luck我试过这个功能,但没有运气
function objectsAreSame(x, y) {
var objectsAreSame = true;
for(var propertyName in x) {
if(x[propertyName] !== y[propertyName]) {
objectsAreSame = false;
break;
}
}
return objectsAreSame;
}
You can iterate through each object in externalLinks
using array#forEach
.您可以使用
array#forEach
遍历externalLinks
每个对象。 Then check for role in the roles
array using array#some
and string#incldues
and update the value of visible
key.然后使用
array#some
和string#incldues
检查roles
数组中的roles
并更新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);
You can use forEach
to loop over external links.您可以使用
forEach
循环外部链接。 Then use some
and includes
to check if role exist.然后使用
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;
}
})
make two loop:做两个循环:
//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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.