I have tried the following but it's throwing an exception:

                if (!$get('sslot_hf0').value in ('X', 'Y', 'Z', '0')) {
                $get('sslot_hf0').value = 'X';
            }

I am looking for a function similar to the IN operator in SQL

有帮助吗?

解决方案 2

You can use below function for the same purpose, second param can be array or object and first param is value you are searching in array or object.

   function inStruct(val,structure)
        {

          for(a in structure)
             {
               if(structure[a] == val)
                 {
                   return true;
                 }
             }
           return false;
        }
if(inStruct('Z',['A','B','Z']))
    {
       //do your stuff
    }

// this function traverse through inherited properties also

i.e in some where your included js libraries

Array.prototype.foo = 10;

than

 instruct(10,[1,2,3]) // will return true

same will happen for objects also. check this fiddle http://jsfiddle.net/rQ8AH/17/

EDITED ::

thank you all for comments ... this is the updated code, I thought it is better to keep old function also. so, some one can notice the difference.

function inStruct(val,structure)
    {

      for(a in structure)
         {

           if(structure[a] == val && structure.hasOwnProperty(a))
             {
               return true;
             }
         }
       return false;
    }

其他提示

You can use indexOf

['X', 'Y', 'Z', '0'].indexOf('Z')
> 2
['X', 'Y', 'Z', '0'].indexOf('T')
> -1

if (['X', 'Y', 'Z', '0'].indexOf($get('sslot_hf0').value) !== -1) {
  //...
}

in doesn't function the same way in Javascript. You'll have to use multiple comparisons splitting them using the || (or OR) operator.

SQL:

something in ('X','Y','Z','0')

Modern JavaScript (including IE>8):

['X','Y','Z','0'].indexOf(something)>-1

More Modern JavaScript (!IE):

['X','Y','Z','0'].includes(something)

If you need a simple includes polyfill for legacy browsers (including IE):

if(!Array.prototype.includes) Array.prototype.includes =function(value,start) {
    start=parseInt(start)||0;
    for(var i=start;i<this.length;i++) if(this[i]==value) return true;
    return false;
};

In deference to AuxTaco’s comment, here is a version of the polyfill which works for IE>8:

if (!Array.prototype.includes) Object.defineProperty(Array.prototype, 'includes', {
    value: function(value,start) {
        start=parseInt(start)||0;
        for(var i=start;i<this.length;i++) if(this[i]==value) return true;
        return false;
    }
});

If you want useful set operation functions and dont mind adding a library, check out underscorejs

Otherwise expect to write for loops to loop over values and perform equality checks.

Create an array

and use jquery.inArray() to check

read here for more http://api.jquery.com/jQuery.inArray/

you can do that, nice and easy, store the values in an array and use IN

  var temparr = ['x', 'y', 'z', '0'];
     if (!$get('sslot_hf0').value in temparr) {
                $get('sslot_hf0').value = 'X';
            }

hope this helps

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top