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

2
  • use of in operator in javascript. I am not sure if the ones you want to achieve can be done with this developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/… Commented Jan 1, 2013 at 5: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. Commented Aug 24, 2018 at 12:07

8 Answers 8

5

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) {
  //...
}
Sign up to request clarification or add additional context in comments.

4 Comments

It gives you the algorithm to include in your pages for that very reason :)
@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?
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, I thought it's related to the IndexOf that's why i asked you, however, it's not related,
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;
    }

5 Comments

This is the best replacement till now, the other answers may works but the browser compatibility is a major. Thnaks @rupesh-patel
If the Array prototype has other properties this code may have unexpected results.
@sachleen, would you please clarify what do you mean by other properties.
console.log(inStruct('T',['A','B','Z'])); Array.prototype.foo = 'T'; console.log(inStruct('T',['A','B','Z'])); - will return false (expected), true (unexpected)
You're right @sachleen, using "for ...in" with array iteration is a bad idea, I have walked through [this question ](stackoverflow.com/questions/500504/…) 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
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;
    }
});

2 Comments

The real array.includes is non-enumerable. Use Object.defineProperty.
@AuxTaco Thanks for the comment. I have added a variation to the answer, though it does require IE>8.
1

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

Comments

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.

Comments

0

Create an array

and use jquery.inArray() to check

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

Comments

0

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

Comments

-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

1 Comment

'z' in ['x', 'y', 'z', '0'] returns false... Read the docs you have to specify a property name, not a value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.