最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

IN Operator in Javascript functions like the one in SQL - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Jan 1, 2013 at 5:13 Alaa AlweishAlaa Alweish 9,08417 gold badges61 silver badges85 bronze badges 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/… – Rab 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. – Manngo Commented Aug 24, 2018 at 12:07
Add a comment  | 

8 Answers 8

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

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

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

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

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/

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

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

发布评论

评论列表(0)

  1. 暂无评论