脑子一片空白,想不出解决办法……
我有以下代码:
if (onlySelected === true) {
if (r.selected == true) {
result.push(pushValue);
}
}
else {
result.push(pushValue);
}
我怎样才能将此代码简化为:
if (condition) { result.push(pushValue) }
脑子一片空白,想不出解决办法……
我有以下代码:
if (onlySelected === true) {
if (r.selected == true) {
result.push(pushValue);
}
}
else {
result.push(pushValue);
}
我怎样才能将此代码简化为:
if (condition) { result.push(pushValue) }
试试这个:
if (!onlySelected || r.selected){
result.push(pushValue);
}
或者如果你绝对需要类型相等而不仅仅是真实性:
if (onlySelected !== true || r.selected){
result.push(pushValue);
}
if(onlySelected === false || r.selected == true) {
result.push(pushValue);
}
(!onlySelected || r.selected) && result.push(pushValue);
if( (onlySelected === true && r.selected === true) || 1) {
result.push(value)
}
我认为这会起作用:
if(!onlySelected || (onlySelected && r.selected))
result.push(pushValue);
嗯,这就是我做到的。
onlySelected === !0 ? r.selected == 1 && result.push(pushValue) : result.push(pushValue)