jQuery API

jQuery.inArray()

jQuery.inArray( value, array ) Returns: Number

Description: Search for a specified value within an array and return its index (or -1 if not found).

  • version added: 1.2jQuery.inArray( value, array )

    valueThe value to search for.

    arrayAn array through which to search.

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.

Example:

Report the index of some elements in the array.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  span { color:red; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	
<div>"John" found at <span></span></div>
<div>4 found at <span></span></div>
<div>"Karl" not found, so <span></span></div>
<script>var arr = [ 4, "Pete", 8, "John" ];

$("span:eq(0)").text(jQuery.inArray("John", arr));
$("span:eq(1)").text(jQuery.inArray(4, arr));
$("span:eq(2)").text(jQuery.inArray("Karl", arr));

</script>
</body>
</html>

Demo:

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for jQuery.inArray() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • Mean Mike
    any chance of there being a case Insensitive option on this function ?
  • I think this method should only return boolean value. period.

    If user cares about the position then indexOf should be used.

    I know this method has been there for a while and jQuery team is not going to change it. In that case it could be renamed to something like existsInArray and keep inArray as private function.
  • Znarkus
    I think this function name is inappropriate, "inArray" somehow assumes a boolean return value. The documentation doesn't show the difference between this and indexOf(), why use this?
  • Array.indexOf() is not supported in every browser. You would use this if you wanted to ensure cross-browser support. Here is the code from the source:

    inArray: function( elem, array ) {
    if ( array.indexOf ) {
    return array.indexOf( elem );
    }

    for ( var i = 0, length = array.length; i < length; i++ ) {
    if ( array[ i ] === elem ) {
    return i;
    }
    }

    return -1;
    }
  • Znarkus
    Then I think the first paragraph should be rephrased, and the function name changed to indexOf (as that is what it does) :)

    It would be cool if jQuery had an inArray()

    inArray: function( elem, array ) {
    return $.indexOf(elem, array) !== -1;
    }
  • Looks like the short description was nested incorrectly in the XML. Now that it's up there, does that clear things up a bit?

    As for changing the function name, it's very unlikely that that would happen with a function that's been in the API for a couple years. But if you feel strongly about it, feel free to report it on the jQuery forum.
  • Znarkus
    Yeah, much better thanks! Maybe something about it wrapping the native indexOf when there's support, for optimization junkies. Thanks for clearing this up!

    Edit: Started a discussion in the forum http://forum.jquery.com/#Topic/14737000000668064
  • What is the performance impact if I use this to find out if the objects in one array is contained in other bigger array. Is there any other faster way to do this?
    Thanks,
    Bharat