0

I get following array from a service (roles of a logged in user) roles = ['MM_VIEW', EM_VIEW]

I have a requirement where I need to check whether the user have any one of the below roles and based on that I need to do some logic MM_VIEW, MM_EDIT, MM_DELETE

So, I was wondering how to proceed with this, to create a enum or directly use OR condition like below

    if (user.roles.includes('MM_VIEW') || user.roles.includes('MM_EDIT') || user.roles.includes('MM_DELETE')) {
      this.appImgPIIError.emit(true);
    } else {
      this.appImgPIIError.emit(false);
    }

This works, but somehow having hard coded values in my component is not seems a good idea to me. ANy suggestions are welcome. Thanks.

1
  • When you are down voting a question please share the reason, it would be helpful when asking questions next time (if the reason is valid). Commented Oct 1, 2020 at 7:47

1 Answer 1

1

You can do it with some method Like below

const roles = ['MM_VIEW', 'EM_VIEW'];
const types = ['MM_VIEW', 'MM_EDIT', 'MM_DELETE'];

if( roles.some(r=>types.includes(r)) ){
    event.emit(true);
} else {
    event.emit(false);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.