Home » vue » Vue/Javascript – the best way to write this ternary

Vue/Javascript – the best way to write this ternary

Posted by: admin November 26, 2021 Leave a comment

Questions:

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?

Answers:

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

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