1 line
1.8 KiB
JSON
1 line
1.8 KiB
JSON
{"name":"jQuery.inArray","type":"method","title":"jQuery.inArray()","deprecated":null,"removed":null,"desc":"Search for a specified value within an array and return its index (or -1 if not found).","categories":["utilities","version/1.2"],"entries":[{"return":"Number","signatures":{"added":"1.2","argument":[{"desc":"The value to search for.","name":"value","type":"Anything"},{"desc":"An array through which to search.","name":"array","type":"Array"},{"desc":"The index of the array at which to begin the search. The default is 0, which will search the whole array.","name":"fromIndex","type":"Number","optional":"true"}]},"examples":{"desc":"Report the index of some elements in the array.","code":"\nvar arr = [ 4, \"Pete\", 8, \"John\" ];\nvar $spans = $( \"span\" );\n$spans.eq( 0 ).text( jQuery.inArray( \"John\", arr ) );\n$spans.eq( 1 ).text( jQuery.inArray( 4, arr ) );\n$spans.eq( 2 ).text( jQuery.inArray( \"Karl\", arr ) );\n$spans.eq( 3 ).text( jQuery.inArray( \"Pete\", arr, 2 ) );\n","css":"\n div {\n color: blue;\n }\n span {\n color: red;\n }\n","html":"\n<div>\"John\" found at <span></span></div>\n<div>4 found at <span></span></div>\n<div>\"Karl\" not found, so <span></span></div>\n<div>\"Pete\" is in the array, but not at or after index 2, so <span></span></div>\n"},"longdesc":"\n <p>The <code>$.inArray()</code> method is similar to JavaScript's native <code>.indexOf()</code> method in that it returns -1 when it doesn't find a match. If the first element within the array matches <code>value</code>, <code>$.inArray()</code> returns 0.</p>\n <p>Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of <code>value</code> within <code>array</code>, we need to check if it's not equal to (or greater than) -1.</p>\n "}]} |