0

脑子一片空白,想不出解决办法……

我有以下代码:

if (onlySelected === true) {
    if (r.selected == true) {
        result.push(pushValue);
    }
}
else {
    result.push(pushValue);
}

我怎样才能将此代码简化为:

if (condition) { result.push(pushValue) } 
4

6 回答 6

8

试试这个:

if (!onlySelected || r.selected){
    result.push(pushValue);
}

或者如果你绝对需要类型相等而不仅仅是真实性:

if (onlySelected !== true || r.selected){
    result.push(pushValue);
}
于 2012-06-08T10:32:29.117 回答
0
if(onlySelected === false || r.selected == true) {
    result.push(pushValue);
}
于 2012-06-08T10:32:40.070 回答
0
(!onlySelected || r.selected) && result.push(pushValue);
于 2012-06-08T10:32:43.837 回答
0
if( (onlySelected === true && r.selected === true) || 1) {
    result.push(value)
}
于 2012-06-08T10:34:10.780 回答
0

我认为这会起作用:

if(!onlySelected || (onlySelected && r.selected))
    result.push(pushValue);
于 2012-06-08T10:34:25.570 回答
0

嗯,这就是我做到的。

onlySelected === !0 ? r.selected == 1 && result.push(pushValue) : result.push(pushValue)
于 2012-11-09T00:09:42.670 回答