GrumpyCrouton
GrumpyCrouton

Reputation: 8621

jQuery inArray giving incorrect results

I'm just trying to check if a string exists in an array of strings.

console.log($.inArray(String(value.value), selectors) > 0, String(value.value), selectors);

The code above is giving me results that look like this

false "23" (2) ["23", "9"]
true "9" (2) ["23", "9"]
false "28" (2) ["23", "9"]
false "20" (2) ["23", "9"]

Without checking if inArray is equal to > 0, my results look like this

0 "23" (2) ["23", "9"]
1 "9" (2) ["23", "9"]
-1 "28" (2) ["23", "9"]
-1 "20" (2) ["23", "9"]

Why is the first one returning as false? How can I make it correctly determine if the string is in the array?

Upvotes: 0

Views: 35

Answers (1)

Tomalak
Tomalak

Reputation: 338148

$.inArray() returns the array position of the found element, or -1.

You wrote > 0, but you meant > -1.

Upvotes: 2

Related Questions