Chirag Sutariya
Chirag Sutariya

Reputation: 362

How can Use IN Operator In Jquery Or Javascript

RatecardIDs=[2, 22, 23, 25];
 if (parseInt(cellValue) in (RatecardIDs)) {
}

How I can use In operator in if condition. in this code firs time it will execute code in if block then it is executing false block.

Upvotes: 0

Views: 2300

Answers (2)

Mritunjay
Mritunjay

Reputation: 25892

In javascript in operator used to get keys in an object. You can use javascript's Array.indexOf or jquery's inArray for that.

Just use like bellow

RatecardIDs=[2, 22, 23, 25];

if(RatecardIDs.indexOf(parseInt(cellValue))>-1){
 ...
}

Or using jquery

if($.inArray(parseInt(cellValue),RatecardsId)>-1){
  ....
}

Upvotes: 1

Lfa
Lfa

Reputation: 1069

The in operator in javascript is for key's in objects.

You can use Array.indexOf() to check if an element exists in the array, it will return -1 if it doesn't exist

Now,

You can check if(RatecardIDs.indexOf(parseInt(cellValue))>-1) {...}

Upvotes: 4

Related Questions