Micheal
Micheal

Reputation: 2322

javascript if condition manipulation

I need some help with my JS logic here. So basically I am getting a bunch of permissions and storing them as follows:

for( var i=0 ; i<permissions.length ; i++ ) {
    p[permissions[i].PERMISSION] = true;
}

The values of the permissions are as follows

p.SELECT_USER,

p.INSERT_USER,

p.UPDATE_USER, etc and so on

Now I want to display/hide a window based on the value of permissions. I need some logic help in my if condition So basically I want something like,

if( 'permissions beigns with the word SELECT' && 'permissions does not begin with any other word' ) {
    this.window.hide()
} else {
    this.window.show()
}

is there any efficient way I cant do this by modifying my for loop?

Upvotes: 0

Views: 88

Answers (1)

KooiInc
KooiInc

Reputation: 122916

if( /^SELECT/.test(/*some permissionvalue*/) ) { /*...*/ } 

would be pretty short and I suppose efficient enough?

Other idea (one liner replaces your code, if hide and show are existing methods contained by this.window):

this.window[ /^SELECT/.test(/*some permissionvalue*/) ? 'hide' : 'show' ]();

/^SELECT/.test(/*some permissionvalue*/) in both suggestion means: test if the given value starts with 'SELECT'. Like @Steve Wang noted, you could replace that using
/*some permission value*/.indexOf('SELECT') === 0 to be even more efficient.

Upvotes: 1

Related Questions