if (currentName === 'WORK') {
dictionary = {
currentName: 'PLAY',
left: { timer: !this.state.timer },
right: { timer: this.state.timer }
}
} else {
dictionary = {
currentName: 'WORK',
left: { timer: this.state.timer },
right: { timer: !this.state.timer }
}
}
是否有更优雅/更紧凑的方式来创建此对象?
答案 0 :(得分:1)
使用三元
可以通过几种不同的方式实现这一目标options mlogic mprint;
%let price_trd = 93.68;
%put &price_trd.;
%let max_price = 100.94;
%put &max_price.;
%macro test;
%if &price_trd. < &max_price %then %do;
%if %sysevalf(&price_trd. LT &max_price) %then %do;
%put Pass HERE;
%end;
%mend;
%test;
或
dictionary = (currentName === 'WORK')
? {
currentName: "PLAY",
left: { timer: !this.state.timer },
right: { timer: this.state.timer }
}
: {
currentName: 'WORK',
left: { timer: this.state.timer },
right: { timer: !this.state.timer }
};
如果你想避免创建一个存储布尔值的变量,那么我的首选是第一个,而且重复性较小。与if / else块相比,它实际上并没有太多保存。
答案 1 :(得分:0)
您可以使用辅助函数来获取布尔值,具体取决于另一个布尔值和检查标志。
function trueFalse(b, v) {
return b ? v : !v;
}
var isWork = currentName === 'WORK',
dictionary = {
currentName: isWork ? 'PLAY' : 'WORK',
left: { timer: trueFalse(isWork, !this.state.timer) },
right: { timer: trueFalse(isWork, this.state.timer) }
};