Web Analytics Made Easy -
StatCounter Check for equivalance to multiple values? - CodingForum

Announcement

Collapse
No announcement yet.

Check for equivalance to multiple values?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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; Sep 9, 2011, 02:21 PM.

    Comment


    • #3
      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;
      }

      Comment


      • #4
        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]( );
        Be yourself. No one else is as qualified.

        Comment


        • #5
          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!

          Comment


          • #6
            More than one way to deprive a feline of its outer integument.

            Especially in programming.

            Most especially in JavaScript.
            Be yourself. No one else is as qualified.

            Comment


            • #7
              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!

              Comment


              • #8
                direct access is simpler than using IN:
                Code:
                if({M:1,S:1,X:1}[SHIP_TYPE]){ ... }
                Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

                Comment


                • #9
                  LOL! Man, that feline keeps getting bigger and bigger. Are we up to the size of a tiger, yet?
                  Be yourself. No one else is as qualified.

                  Comment


                  • #10
                    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!

                    Comment

                    Working...
                    X
                    😀
                    🥰
                    🤢
                    😎
                    😡
                    👍
                    👎