0

i have a object like:

["1", "2", "1", "2"] 

No i want to check if "1" exists in object. I don't need to know how often it exists.

$.inArray(1, myObject) -> Returns always -1

So what is wrong or what i have to do?

2 Answers 2

2

You're checking for an integer, when the array contains only strings. Also be advised that jQuery's inArray() function does not act like its PHP counterpart. Rather than returning true or false, it returns the actual zero-based index of the value if found (as an integer), or -1 if it isn't in the array.

Try this:

if($.inArray('1', myObject) >= 0) { // Do your stuff. } 

Here's a jsFiddle demo

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

1 Comment

Works great. I think my Problem was to search for integer. Thank you!
2

Or you can use plain JS:

var x = ["1", "2", "1", "2"]; if (x.indexOf("1") !== -1) { console.log("found"); } else { console.log("not found"); } 

1 Comment

Thank you too but i use the jQuery-Funkction.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.