2013-05-23 8 views
7

私は、switch文を使ってオブジェクトのプロパティ値が "真実"であるかどうかを調べようとしています。switch文の真理を評価する

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" 

これを行うための適切な方法は何ですか?このサンプル・ブロックを使用して

+0

は、あなたが "truthy" とはどういう意味ですか? – James

+0

James:http://www.sitepoint.com/javascript-truthy-falsy/は真実と偽の概念を説明しています。 –

答えて

12

あなたはこれを行うことができます。

case !!test.foo: 

これは、ブールへの変換を強制します。ブール値を強制するために!!を使用することに加えて

+0

ニースのヒント。私はそれを知らなかった – FLX

+0

これに返信するのを忘れました。私は良いole double equalsについて忘れてしまった。リフレッシュしてくれてありがとう! –

-1

、あなたがtruthy/falsyを評価するためにswitch声明でこれを行うことができます:あなたはあなたの自己のために見ることができるようにここで

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]); 
    } 
}()); 

そして、:)、義務jsFiddle: http://jsfiddle.net/AE8MU/

+0

詳細返信peteありがとう。私はより軽い方が好きです!!しかしこれも素晴らしい解決策です。 –

関連する問題