Reputation: 67
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?
Upvotes: 0
Views: 2219
Reputation: 53198
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
Upvotes: 2
Reputation: 1715
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");
}
Upvotes: 2