1 line
3.5 KiB
JSON
1 line
3.5 KiB
JSON
{"name":"jQuery.each","type":"method","title":"jQuery.each()","deprecated":null,"removed":null,"desc":"A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.","categories":["utilities","version/1.0"],"entries":[{"return":"Object","signatures":{"added":"1.0","argument":[{"desc":"The object or array to iterate over.","name":"collection","type":"Object"},{"desc":"The function that will be executed on every object.","name":"callback(indexInArray, valueOfElement)","type":"Function"}]},"examples":[{"desc":"Iterates through the array displaying each number as both a word and numeral","code":"\nvar arr = [ \"one\", \"two\", \"three\", \"four\", \"five\" ];\nvar obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };\n\njQuery.each( arr, function( i, val ) {\n $( \"#\" + val ).text( \"Mine is \" + val + \".\" );\n\n // Will stop running after \"three\"\n return ( val !== \"three\" );\n});\n\njQuery.each( obj, function( i, val ) {\n $( \"#\" + i ).append( document.createTextNode( \" - \" + val ) );\n});\n","css":"\n div {\n color: blue;\n }\n div#five {\n color: red;\n }\n","html":"\n<div id=\"one\"></div>\n<div id=\"two\"></div>\n<div id=\"three\"></div>\n<div id=\"four\"></div>\n<div id=\"five\"></div>\n"},{"desc":"Iterates over items in an array, accessing both the current item and its index.","code":"\n$.each( [ \"a\", \"b\", \"c\" ], function( i, l ){\n alert( \"Index #\" + i + \": \" + l );\n});\n"},{"desc":"Iterates over the properties in an object, accessing both the current item and its key.","code":"\n$.each({ name: \"John\", lang: \"JS\" }, function( k, v ) {\n alert( \"Key: \" + k + \", Value: \" + v );\n});\n"}],"longdesc":"\n <p>The <code>$.each()</code> function is not the same as <a href=\"/each/\">$(selector).each()</a>, which is used to iterate, exclusively, over a jQuery object. The <code>$.each()</code> function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the <code>this</code> keyword, but Javascript will always wrap the <code>this</code> value as an <code>Object</code> even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.</p>\n <pre><code>\n$.each([ 52, 97 ], function( index, value ) {\n alert( index + \": \" + value );\n});\n </code></pre>\n <p>This produces two messages:</p>\n <p>\n <samp>0: 52</samp>\n <br/>\n <samp>1: 97</samp>\n </p>\n <p>If an object is used as the collection, the callback is passed a key-value pair each time:</p>\n <pre><code>\nvar obj = {\n \"flammable\": \"inflammable\",\n \"duh\": \"no duh\"\n};\n$.each( obj, function( key, value ) {\n alert( key + \": \" + value );\n});\n </code></pre>\n <p>Once again, this produces two messages:</p>\n <p>\n <samp>flammable: inflammable</samp>\n <br/>\n <samp>duh: no duh</samp>\n </p>\n <p>We can break the <code>$.each()</code> loop at a particular iteration by making the callback function return <code>false</code>. Returning <em>non-false</em> is the same as a <code>continue</code> statement in a for loop; it will skip immediately to the next iteration.</p>\n "}]} |