7

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

Alaa Alweish
  • 8,904
  • 16
  • 57
  • 84
  • use of `in` operator in javascript. I am not sure if the ones you want to achieve can be done with this https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/in – Raab Jan 01 '13 at 05:27
  • For the sake of completness, `in` in JavaScript is used to test whether a property exists in an Object. Things are further complicated by the fact that JavaScript arrays are actually specialised objects, so using `in` with an array is valid, but probably not what you have in mind. It tests whether something is a valid property of the array object, but not whether it is an existing value in the array itself. – Manngo Aug 24 '18 at 12:07

7 Answers7

4

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) {
  //...
}
sachleen
  • 30,730
  • 8
  • 78
  • 73
  • It gives you the algorithm to include in your pages for that very reason :) – sachleen Jan 01 '13 at 05:44
  • @sachleen, thanks for your answer, it seems that `indexof` is a good replacement for `IN` functionality like in `SQL`, however, the code you used is throwing an exception `$get is not defined`, do you have explanation why `indexof` is not recognizing the '$get' function? – Alaa Alweish Jan 01 '13 at 06:00
  • That's your code, not mine. See the first two lines and the doc for usage of the function. If `$get` is not defined here, it's not defined in your question's code either. – sachleen Jan 01 '13 at 06:12
  • @sachleen, I thought it's related to the `IndexOf` that's why i asked you, however, it's not related, – Alaa Alweish Jan 01 '13 at 06:17
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;
    }
Rupesh Patel
  • 3,015
  • 5
  • 28
  • 49
  • 1
    This is the best replacement till now, the other answers may works but the browser compatibility is a major. Thnaks @rupesh-patel – Alaa Alweish Jan 01 '13 at 09:47
  • If the Array prototype has other properties this code may have unexpected results. – sachleen Jan 01 '13 at 19:00
  • @sachleen, would you please clarify what do you mean by other properties. – Alaa Alweish Jan 01 '13 at 19:21
  • 1
    `console.log(inStruct('T',['A','B','Z'])); Array.prototype.foo = 'T'; console.log(inStruct('T',['A','B','Z']));` - will return `false` (expected), `true` (unexpected) – sachleen Jan 01 '13 at 19:24
  • 1
    You're right @sachleen, using "for ...in" with array iteration is a bad idea, I have walked through [this question ](http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays) one solution is to replace the `inStruct` with the following : `function inStruct(val,structure)` `{` `for(var i = 0; i < structure.length; i += 1)` `{` `if(structure[i] == val)` `{` `return true;` `}` `}` `return false;` `}` Thanks for your contribution sachleen , I hope @Rupesh updates this answer – Alaa Alweish Jan 01 '13 at 20:07
2

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;
    }
});
Manngo
  • 14,066
  • 10
  • 88
  • 110
1

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

jeremy
  • 9,965
  • 4
  • 39
  • 59
0

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.

Michael Christensen
  • 1,768
  • 1
  • 13
  • 11
0

Create an array

and use jquery.inArray() to check

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

Adarsh Raj
  • 291
  • 1
  • 7
-1

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

Ayhaab abd
  • 37
  • 4
  • 1
    `'z' in ['x', 'y', 'z', '0']` returns false... Read the [docs](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/in) you have to specify a property name, not a value. – sachleen Jan 01 '13 at 05:37