Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check for equivalance to multiple values?

    Hi all,
    I have to check for several possible values of a variable and I currently do it like so:
    Code:
    (SHIP_TYPE == "M" || SHIP_TYPE == "S" || SHIP_TYPE == "X")
    But this seems overly manual to me. I'm imagining something like the SQL "IN" function:
    Code:
    SHIP_TYPE IN ('M','S','X')
    Does such a thing exist in JavaScript, or do I need to write my own function?

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You can use a regular expression-
    Code:
    if(/^[MSX]$/.test(SHIP_TYPE)){
    There is an 'in' operator, but you have to create an object and instantiate all the properties:

    Code:
    var SHIP_TYPE='S';
    if(SHIP_TYPE in {M:1,S:1,X:1}){
    Last edited by mrhoo; 09-09-2011 at 07:21 PM.

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Something to consider...

    How about:
    Code:
    if ('MSX'.indexOf(SHIP_TYPE) != -1) { alert('Found match') } else { alert('NO match found'); }
    The position of the match could also be used in a switch statement
    Code:
    var posn = 'MSX'.indexOf(SHIP_TYPE);
    switch (posn) {
      case 0: do_something( 'M'); break;
      case 1: do_something( 'S'); break;
      case 2: do_something( 'X'); break;
      default : alert('No match found'); break;
    }

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    I like if ('MSX'.indexOf(SHIP_TYPE) != -1) ... as being the simplest, but if we are tossing around ideas...

    Code:
    function doMthing( ) { ... }
    function doSthing( ) { ... }
    function doXthing( ) { ... }
    
    var actions = { "M" : doMthing, "S" : doSthing, "X" : doXthing };
    if ( SHIP_TYPE in actions ) actions[SHIP_TYPE]( );
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    jmrker (09-09-2011)

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Thumbs up

    Quote Originally Posted by Old Pedant View Post
    I like if ('MSX'.indexOf(SHIP_TYPE) != -1) ... as being the simplest, but if we are tossing around ideas...

    Code:
    function doMthing( ) { ... }
    function doSthing( ) { ... }
    function doXthing( ) { ... }
    
    var actions = { "M" : doMthing, "S" : doSthing, "X" : doXthing };
    if ( SHIP_TYPE in actions ) actions[SHIP_TYPE]( );
    Well, that's pretty neat too!

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    More than one way to deprive a feline of its outer integument.

    Especially in programming.

    Most especially in JavaScript.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Smile

    Quote Originally Posted by Old Pedant View Post
    More than one way to deprive a feline of its outer integument.

    Especially in programming.

    Most especially in JavaScript.
    I guess we can not just "skin a cat" anymore with PETA present!

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,294
    Thanks
    10
    Thanked 583 Times in 564 Posts
    direct access is simpler than using IN:
    Code:
    if({M:1,S:1,X:1}[SHIP_TYPE]){ ... }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    LOL! Man, that feline keeps getting bigger and bigger. Are we up to the size of a tiger, yet?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Wow, thank you all! I had no idea there was an "in" operator. And the direct access concept is new to me as well. So many good options. I'll play around with all of these. Much fun!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •