Vue/Javascript - 写这个三元的最好方法

Vue/Javascript - the best way to write this ternary

我正在寻找最有效的方式来编写这个三进制。

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

这是在 vuejs 方法中,但我想这无关紧要。

我应该复制到本地 var 以便它不会多次读取吗?

let activeGroup = this.mygroup

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

还有什么吗?

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

您将 this.mygroup 分配给 ''id,这是您想要的