0

I have an Array and I'm trying to use inArray() check if an item is not in the Array

var popUpArray = ['join','reset_password','photos','session_expired'];


if($.inArray(urlType, popUpArray) != 0) {
  process code...
}

Basically I want to process the code within the if statement if urlType variable doesn't match one of the values in the array.

It works on the first value 'join' but not on the others. I understand this is because as soon as it doesn't match on one item it progresses.

I wondering how I could re-write this so it would only progress into the IF statement if the var urlType didn't match ANY of the items in the array.

thx

1 Answer 1

2

It works with join because it's the zero index. Try this:

if($.inArray(urlType, popUpArray) == -1) {

}

Per the docs:

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.

Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.

http://api.jquery.com/jQuery.inArray/

Sign up to request clarification or add additional context in comments.

Comments

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.