-2

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

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;
}
Miomir Dancevic
  • 6,726
  • 15
  • 74
  • 142
  • the roles array values and the externalLinks array of objects property role should be equal isn't in your question no object property match any of the roles array – Learner Feb 07 '19 at 09:21
  • AnyRulesGo@admin it should be @admin only, then only we can check the property – Learner Feb 07 '19 at 09:21
  • I need to compare does role value in active, exist somehow in array roles? – Miomir Dancevic Feb 07 '19 at 09:23
  • @DILEEPTHOMAS actually they dont, you can check if one string _contains_ another – mast3rd3mon Feb 07 '19 at 09:24
  • @mast3rd3mon then we need to split the roles each one , like "NoRules@admin" will be "NoRules" , "@admin" – Learner Feb 07 '19 at 09:29
  • @DILEEPTHOMAS why do you need to split it? using a `contains` doesnt care if there are extra characters in the same string – mast3rd3mon Feb 07 '19 at 09:33
  • @mast3rd3mon is there any array.contains methods is there – Learner Feb 07 '19 at 09:35
  • @mast3rd3mon you need to use array.some and inside that you need to array.includes method. Then it will resolve – Learner Feb 07 '19 at 09:36
  • @DILEEPTHOMAS you dont have to, if you read my previous comment, i said to see if a *string* _contains_ another, as there is a `stringVar.contains(searchStringVar);` method – mast3rd3mon Feb 07 '19 at 09:37

3 Answers3

2

You can iterate through each object in externalLinks using array#forEach. Then check for role in the roles array using array#some and string#incldues and update the value of visible key.

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);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • using `contains` would be the better/correct method, as it appears only firefox makes use of the `includes` method – mast3rd3mon Feb 07 '19 at 09:41
  • You can refer to this [answer](https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript). Please share the link of `contains` method. – Hassan Imam Feb 07 '19 at 10:12
1

You can use forEach to loop over external links. Then use some and includes to check if role exist.

roles = ['AnyRulesGo@admin', 'NoRules@admin', 'HowYesNo@history', 'MamaMia@survey'];

externalLinks.forEach((extLink) => {
  if(roles.some(role => role.includes(extLink.role))){
    extLink.visible = true;
  }
})
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
  • can you add the roles array in your answer . – Learner Feb 07 '19 at 09:31
  • if you are using the roles array of question i hope the above snippet wont work, since the roles array should be equal to the extLink.role – Learner Feb 07 '19 at 09:32
  • I don't understand what you mean. OP asked to check if `roles` array contains `externalLink` role. So, this is how it is done. – Harun Yilmaz Feb 07 '19 at 09:36
  • before editing , there was no array.some and array.includes. After the edit, now it will work – Learner Feb 07 '19 at 09:37
  • Before editing, there were `includes`, but not mentioned using `some` (code existed). But that's not the point. Of course I answered the question based on the data provided by OP. There is no need to write the data again, I think. If you think that this snippet will not work, so tell me how is this answer different than the only upvoted answer? – Harun Yilmaz Feb 07 '19 at 09:40
  • @DILEEPTHOMAS the `role.includes(extLink.role)` is just the same as `role.contains(extLink.role)` – mast3rd3mon Feb 07 '19 at 09:40
-1

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)
Eliseo
  • 50,109
  • 4
  • 29
  • 67