Javascript 在switch语句中计算Truthy

Javascript 在switch语句中计算Truthy,javascript,Javascript,我试图通过switch语句确定对象属性值是否为“truthy” 使用此示例块: var test = { foo: "bar" } switch(true) { case test.foo: console.log("success in switch"); break default: console.log("no success in switch"); break } if (test.foo) { console.log("succe

我试图通过switch语句确定对象属性值是否为“truthy”

使用此示例块:

var test = {
  foo: "bar"
}

switch(true) {
  case test.foo:
    console.log("success in switch");
    break
  default:
    console.log("no success in switch");
    break
}

if (test.foo) {
  console.log("success in if");
} else {
  console.log("no success in if");
}
结束日志记录:

"no success in switch"
"success in if"
正确的方法是什么?

您可以这样做:

case !!test.foo:

这将强制转换为布尔值。

除了使用
要强制布尔值,可以使用
switch
语句来计算truthy/falsy:

switch (true) {             // use a boolean to force case statement to evaluate conditionals
case (val ? true : false):  // force a truthy/falsy evaluation of val using parentheses and the ternary operator
    console.log(val + ' evaluates as truthy in the switch statement.');
    break;
default:
    console.log(val + ' evaluates as falsy in the switch statement.');
    break;
}
以下是一组功能和测试,您可以自己查看:

(function () {
    'use strict';
    var truthitizeSwitch = function (val) {
            switch (true) {             // use a boolean to force case statement to evaluate conditionals
            case (val ? true : false):  // force a truthy/falsy evaluation of val using parentheses and the ternary operator
                console.log(val + ' evaluates as truthy in the switch statement.');
                break;
            default:
                console.log(val + ' evaluates as falsy in the switch statement.');
                break;
            }
            return !!val;   // use !! to return a coerced boolean
        },
        truthitizeIf = function (val) {
            if (val) {      // if statement naturally forces a truthy/falsy evaluation
                console.log(val + ' evaluates as truthy in the if statement.');
            } else {
                console.log(val + ' evaluates as falsy in the if statement.');
            }
            return !!val;   // use !! to return a coerced boolean
        },
        tests = [
            undefined,                              // falsey: undefined
            null,                                   // falsey: null
            parseInt('NaNificate this string'),     // falsey: NaN
            '',                                     // falsey: empty string
            0,                                      // falsey: zero
            false,                                  // falsey: boolean false
            {},                                     // truthy: empty object
            {"foo": "bar"},                         // truthy: non-empty object
            -1,                                     // truthy: negative non-zero number
            'asdf',                                 // truthy: non-empty string
            1,                                      // truthy: positive non-zero number
            true                                    // truthy: boolean true
        ],
        i;
    for (i = 0; i < tests.length; i += 1) {
        truthitizeSwitch(tests[i]);
        truthitizeIf(tests[i]);
    }
}());
(函数(){
"严格使用",;
var truthitizeSwitch=功能(val){
switch(true){//使用布尔值强制case语句计算条件
case(val?true:false)://使用括号和三元运算符强制对val进行truthy/falsy求值
log(val+'在switch语句中计算为truthy');
打破
违约:
log(val+'在switch语句中计算为falsy');
打破
}
return!!val;//使用!!返回强制布尔值
},
truthitizeIf=函数(val){
if(val){//if语句自然会强制执行truthy/falsy评估
log(val+'在if语句中计算为truthy');
}否则{
log(val+'在if语句中计算为falsy');
}
return!!val;//使用!!返回强制布尔值
},
测试=[
未定义,//错误:未定义
null,//false:null
parseInt('NaNificate this string'),//false:NaN
'',//false:空字符串
0,//false:0
false,//false:boolean false
{},//truthy:空对象
{“foo”:“bar”},//truthy:非空对象
-1,//truthy:非零负数
'asdf',//truthy:非空字符串
1,//truthy:正非零数
true//truthy:boolean true
],
我
对于(i=0;i

当然还有:),必修的jsFiddle:

你说的“truthy”是什么意思?詹姆斯:解释了truthy和falsy的概念。很好的提示。我不知道,谢谢你的详细回复,皮特。我喜欢简洁的!!,但这也是一个很好的解决方案。