Alaa Alweish
Alaa Alweish

Reputation: 9084

IN Operator in Javascript functions like the one in SQL

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

Upvotes: 7

Views: 6390

Answers (8)

P Zia
P Zia

Reputation: 1

combine filter with find. somthing like this:

  let authors = [
  {
    name: "t1",
    code: 15,
  },
  {
    name: "t2",
    code: 25,
  },
  {
    name: "t3",
    code: 30,
  },
  {
    name: "t4",
    code: 35,
  },
  {
    name: "t5",
    code: 35,
  },
];

let codes = [25, 35];

let selectedAuthors = authors.filter((a) => codes.find((b) => b === a.code));

console.log(selectedAuthors);

Upvotes: 0

Manngo
Manngo

Reputation: 16291

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;
    }
});

Upvotes: 2

Rupesh Patel
Rupesh Patel

Reputation: 3065

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;
    }

Upvotes: 2

Adarsh Raj
Adarsh Raj

Reputation: 301

Create an array

and use jquery.inArray() to check

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

Upvotes: 0

Michael Christensen
Michael Christensen

Reputation: 1786

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.

Upvotes: 0

sachleen
sachleen

Reputation: 31131

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) {
  //...
}

Upvotes: 5

Ayhaab abd
Ayhaab abd

Reputation: 37

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

Upvotes: -1

jeremy
jeremy

Reputation: 10057

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

Upvotes: 1

Related Questions