jQuery.inArray( value, array [ | jquery array
Description:Searchforaspecifiedvaluewithinanarrayandreturnitsindex(or-1ifnotfound).The$.inArray()methodissimilartoJavaScriptsnative.indexOf()methodinthatitreturns-1whenitdoesntfindamatch.Ifthefirstelementwithinthearraymatchesvalue,$.inArray()returns0.BecauseJavaScripttreats0aslooselyequaltofalse(i.e.0==false,but0!==false),tocheckforthepresenceofvaluewithinarray,youneedtocheckifitsnotequalto(orgreaterthan)-1.Thecomparisonbetweenvaluesisstrict.Thefollowingwillreturn-1(notfound)becauseanumberis...
Description: Search for a specified value within an array and return its index (or -1 if not found).
The $.inArray() method is similar to JavaScripts native .indexOf() method in that it returns -1 when it doesnt 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), to check for the presence of value within array, you need to check if its not equal to (or greater than) -1.
The comparison between values is strict. The following will return -1 (not found) because a number is being searched in an array of strings:
1
$.inArray( 5 + 5, [ "8", "9", "10", 10 + "" ] );
Example:Report the index of some elements in the array.
1
2
3
4
5
6
...