jQuery.inArray()Method

jQuery 杂项方法jQuery Miscellaneous Methods

Examples

Returns the index value of the specified element in the array

<div>"John", at index value<span></span>was found at the position</div> <div>4, at index value<span></span>was found at the position</div> <div>"Karl" was not found, so it returns<span></span> </div> <div>"Pete" is in the array, but not at a position with index greater than 2, so it returns<span></span></div>
<script> $(function () { var arr = [ 4, "Pete", 8, "John" ]; var $spans = $( "span" ); $spans.eq( 0 ).text( jQuery.inArray( "John", arr ) ); $spans.eq( 1 ).text( jQuery.inArray( 4, arr ) ); $spans.eq( 2 ).text( jQuery.inArray( "Karl", arr ) ); $spans.eq( 3 ).text( jQuery.inArray( "Pete", arr, 2 ) ); }) </script>

Try it yourself »

Definition and Usage

The $.inArray() function is used to search for a specified value in an array and returns its index value (if not found, returns -1)

Tip:The source array is not affected; the filter result is only reflected in the returned result array.


Syntax

$.inArray( value, array [, fromIndex ] )

Parameter Description
value Any type The value to search for.
array Array type Specifies the array to be searched.
fromIndex Optional. Number type Specifies to start searching from the specified index position of the array, defaulting to 0.


jQuery 杂项方法jQuery Miscellaneous Methods

Other Extensions