9

次の略記はありますか-

if(tld == "com" || tld == "net" || tld == "co" || tld == "org" || tld == "info" || tld == "biz")
{
    //do something;
}
4

4 に答える 4

21

配列を使用できます

if(["","com","net","co","org","info","biz"].indexOf(tld) > -1) {
     // do something
}

またはjqueryを使用している場合:

$.inArray(tld, ["com","net","co","org","info","biz"])

REF- OR操作(||)とinArray()のパフォーマンス

于 2012-06-20T20:38:33.053 に答える
12

正規表現を使用します。

if ( /^(com|net|co|org|info|biz)$/i.test(tld) ) {
    // do something
}
于 2012-06-20T20:37:11.307 に答える
0

switchステートメントの使用について考えましたか?このようなもの:

switch(tld)
{
   case 'com':
   case 'net':
   case 'co':
   ...
   ...
   // do something for all of them
   break;
   default:
   // if you want you can have default process here
   break;
}
于 2012-06-20T20:38:13.617 に答える
0

Bitwise IndexOf Shorthand

if (~["com", "net", "co", "org", "info", "biz"].indexOf(method)){/*Do somthing if true*/}
if (!~["com", "net", "co", "org", "info", "biz"].indexOf(method)){/*Do somthing if false*/}
于 2019-11-22T13:33:37.313 に答える