Hi All,
I want to use IN Operator in Javascript
e.g : if (cType IN ('SK','SK 2'...)
Regards
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sandeep Singh ShekhawatPosted Mar 27, 2013, 2:45 AM
function getValue()
{
var typesArray = new Array("SK", "SK 2", "SK 4","XYZ TYPE");
var cType = 'XYZ TYPE';
for (var j = 0; j < typesArray.length; j++)
{
if (typesArray[j].match(cType))
{
alert("It exists in array");
}
}
}
ShehzadPosted Mar 27, 2013, 3:34 AM
I have done some modification as per my requirement
var typesArray = new Array("SK", "SK 2", "SK 4","XYZ TYPE");
var cType = 'SK 2';
for (var j = 0; j < typesArray.length; j++)
{
if (typesArray[j] == cType)
{
alert("It exists in array");
break;
}
}
ShehzadPosted Mar 27, 2013, 2:48 AM
Regards
ShehzadPosted Mar 27, 2013, 2:23 AM
for e.g. : cType = "XYZ TYPE" // cType will will be assigned as per user input from InterFace.
How will i check whether "XYZ TYPE" is exists or not in typesArray?
Regards
Sandeep Singh ShekhawatPosted Mar 27, 2013, 1:21 AM
Hi Shehzad,
You can't use NOT IN operator in if condition because javascript doesn't support it. If you want use NOT IN then you can use like following code:
function NotInOperator()
{
var typesArray = new Array("SK", "SK 2", "SK 4");
var cType = 4;
if (!(cType in typesArray))
{
alert("It does not exist in array");
}
}
Thanks
Sandeep
ShehzadPosted Mar 27, 2013, 1:07 AM
Index will not work for cType NOT IN
Regards
Sandeep Singh ShekhawatPosted Mar 27, 2013, 12:27 AM
for example:
<script type="text/javascript">
function InOperator()
{
var typesArray = new Array("SK", "SK 2", "SK 4");
var cType=1;
if (cType in typesArray)
{
alert("It exists in array");
}
}
script>
Thanks
Sandeep Shekhawat