TommyD
TommyD

Reputation: 1043

Vue/Javascript - the best way to write this ternary

I am looking for the most efficient way to write this ternary.

id === this.mygroup ? this.mygroup = ''  : this.mygroup = id;

This is in a vuejs method but I suppose it does not matter.

Should I copy to a local var so it does not read it multiple times?

let activeGroup = this.mygroup

 id === activeGroup ? activeGroup = ''  : activeGroup = id;

Anything else?

Upvotes: 0

Views: 113

Answers (1)

Sebastian Berglönn
Sebastian Berglönn

Reputation: 4278

this.mygroup = id === this.mygroup ? '' : id;

You assign this.mygroup to either '' or id, which is what you want

Upvotes: 3

Related Questions