vamsi
vamsi

Reputation: 1586

Best way to find a matching value from an array with another set of values using Angular

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.

Upvotes: 0

Views: 468

Answers (1)

mkHun
mkHun

Reputation: 5927

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);
}

Upvotes: 1

Related Questions