<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Learning jQuery &#187; Utilities</title>
	<atom:link href="http://www.learningjquery.com/category/types/utilities/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learningjquery.com</link>
	<description>Tips, techniques, and tutorials for the jQuery JavaScript library</description>
	<lastBuildDate>Tue, 17 Jan 2012 14:14:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>jQuery.map() in 1.6</title>
		<link>http://www.learningjquery.com/2011/05/jquery-map-in-16</link>
		<comments>http://www.learningjquery.com/2011/05/jquery-map-in-16#comments</comments>
		<pubDate>Tue, 03 May 2011 20:39:43 +0000</pubDate>
		<dc:creator>Jordan Boesch</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=1287</guid>
		<description><![CDATA[Among all of the great fixes and additions to jQuery 1.6, I'm happy to say that jQuery.map() now supports objects! The previous map only supported arrays. With other libraries already offering object support for map, it was a nice addition. Let's say you want to collect an array of object keys from a JSON object. [...]]]></description>
			<content:encoded><![CDATA[<p>
Among all of the <a href="http://blog.jquery.com/2011/05/03/jquery-16-released/" target="_blank">great fixes and additions</a> to jQuery 1.6, I'm happy to say that <a href="http://api.jquery.com/jquery.map/" target="_blank">jQuery.map()</a> now supports objects!  The previous map only supported arrays. With <a href="http://documentcloud.github.com/underscore/#map" target="_blank">other libraries</a> already offering object support for map, it was a nice addition.</p><span id="more-1287"></span>
<p>
Let's say you want to collect an array of object keys from a JSON object.  
</p>
<div class="igBar"><span id="ljavascript-12"><a href="#" onclick="javascript:showPlainTxt('javascript-12'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-12">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> myObj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jordan&quot;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #3366CC;">&quot;hair_color&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;brown&quot;</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #3366CC;">&quot;eye_color&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ravishing&quot;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span>;</div></li>
</ol></div>
</div></div><br />

<p>Here's how you <em>would</em> have done it in older versions of jQuery (prior to 1.6):</p>

<div class="igBar"><span id="ljavascript-13"><a href="#" onclick="javascript:showPlainTxt('javascript-13'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-13">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> objKeys <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.each"><span style="">$.<span style="color: #660066;">each</span></span></a><span style="color: #009900;">&#40;</span> myObj<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> key<span style="color: #339933;">,</span> value <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; objKeys.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// objKeys == [ &quot;name&quot;, &quot;hair_color&quot;, &quot;eye_color&quot; ]</span></div></li>
</ol></div>
</div></div><br />

<p>Here's the new way (just a little more convenient):</p>

<div class="igBar"><span id="ljavascript-14"><a href="#" onclick="javascript:showPlainTxt('javascript-14'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-14">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> objKeys <span style="color: #339933;">=</span> <a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.map"><span style="">$.<span style="color: #660066;">map</span></span></a><span style="color: #009900;">&#40;</span> myObj<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> value<span style="color: #339933;">,</span> key <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> key;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// objKeys == [ &quot;name&quot;, &quot;hair_color&quot;, &quot;eye_color&quot; ]</span></div></li>
</ol></div>
</div></div><br />

<h4>Going inside the new jQuery.map()</h4>
<p>
For those that are curious and a little more advanced, there are some neat things going on under the hood of the new <a href="http://api.jquery.com/jQuery.map/" target="_blank">jQuery.map()</a> that I would like to talk about.
</p>
<p>
Adding object support seems pretty trivial at first since <a href="http://api.jquery.com/jQuery.each/" target="_blank">jQuery.each()</a> is already doing it - so it must be an easy patch, right? Well, not really. Let's look at how jQuery.each() is doing it.  If you <a href="https://github.com/jquery/jquery/blob/master/src/core.js#L591" target="_blank">look at the jQuery source</a> on github, you'll see that it's doing: 
</p>

<div class="igBar"><span id="ljavascript-15"><a href="#" onclick="javascript:showPlainTxt('javascript-15'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-15">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">length <span style="color: #339933;">=</span> object.<span style="color: #660066;">length</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">isObj <span style="color: #339933;">=</span> length <span style="color: #339933;">===</span> undefined <span style="color: #339933;">||</span> jQuery.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span> object <span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>
Can you see the flaw in this? It's going to treat the variable <em>object</em> (could be an array or object) like an object if length is undefined.  What happens when I have an object with a "length" property?  <a href="http://jsfiddle.net/jboesch26/r5aAS/" target="_blank">It dies a horrible death</a>.  Some have reported this <a href="http://bugs.jquery.com/ticket/7260" target="_blank">issue</a>.
</p>
<p>
For the new jQuery.map(), we wanted support for objects and <em>also</em> be able to pass an object with a length property and not have it blow up like jQuery.each() does. <a href="https://github.com/jquery/jquery/pull/299/files#L0R713" target="_blank">Dan Heberden came to the rescue</a>. <a href="http://danheberden.com/" target="_blank">Dan</a> spent some time making sure that jQuery.map() didn't face the same fate while keeping performance in mind.
</p>
<p>
Here is what Dan did to see if <em>elems</em> is an array:
</p>
<div class="igBar"><span id="ljavascript-16"><a href="#" onclick="javascript:showPlainTxt('javascript-16'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-16">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">length <span style="color: #339933;">=</span> elems.<span style="color: #660066;">length</span><span style="color: #339933;">,</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">isArray <span style="color: #339933;">=</span> elems <span style="color: #000066; font-weight: bold;">instanceof</span> jQuery <span style="color: #339933;">||</span> length <span style="color: #339933;">!==</span> undefined &#038;& <span style="color: #000066; font-weight: bold;">typeof</span> length <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;number&quot;</span> &#038;& <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> length<span style="color: #339933;">&gt;</span> <span style="color: #CC0000;color:#800000;">0</span> &#038;& elems<span style="color: #009900;">&#91;</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#93;</span> &#038;& elems<span style="color: #009900;">&#91;</span> length <span style="color: #339933;">-</span><span style="color: #CC0000;color:#800000;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> length <span style="color: #339933;">===</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #339933;">||</span> jQuery.<span style="color: #660066;">isArray</span><span style="color: #009900;">&#40;</span> elems <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ;</div></li>
</ol></div>
</div></div><br />

<p>I should note that John Resig <a href="https://github.com/jquery/jquery/commit/6c449fd5df3e0ec50e893d055da9aea486e7d71c/" target="_blank">added</a>:</p>

<div class="igBar"><span id="ljavascript-17"><a href="#" onclick="javascript:showPlainTxt('javascript-17'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-17">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #339933;">||</span> length <span style="color: #339933;">===</span> <span style="color: #CC0000;color:#800000;">0</span></div></li>
</ol></div>
</div></div><br />

<p>To support <a href="http://bugs.jquery.com/ticket/8993" target="_blank">empty node lists</a>.</p>

<p>
Let's look at the first part of our isArray variable.  You'll notice it does a check for:
</p>
<div class="igBar"><span id="ljavascript-18"><a href="#" onclick="javascript:showPlainTxt('javascript-18'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-18">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">elems <span style="color: #000066; font-weight: bold;">instanceof</span> jQuery</div></li>
</ol></div>
</div></div><br />
<p>
We're checking to see if <em>elems</em> is an instanceof jQuery.  We're doing this first because you're most likely dealing with a <a href="http://api.jquery.com/category/properties/jquery-object-instance-properties/" target="_blank">jQuery collection/array-like object</a>. Since jQuery collections are treated like regular arrays (uses for loop), this is an optimized way of checking if we should treat <em>elems</em> like an array.  Below is an example of <a href="http://api.jquery.com/map/" target="_blank">jQuery.fn.map()</a> in action.
</p>
<div class="igBar"><span id="ljavascript-19"><a href="#" onclick="javascript:showPlainTxt('javascript-19'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-19">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=map"><span style="">map</span></a><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> i<span style="color: #339933;">,</span> element <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// under the hood, this call to map passes the </span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// isArray variable check right away because </span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// $('div') is an instance of jQuery</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// using 'this' here refers to the DOM element</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>
<a href="http://jsfiddle.net/jboesch26/aBkmr/" target="_blank">Here is</a> jQuery.map() with a jQuery collection passed as the first argument:
</p>

<div class="igBar"><span id="ljavascript-20"><a href="#" onclick="javascript:showPlainTxt('javascript-20'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-20">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.map"><span style="">$.<span style="color: #660066;">map</span></span></a><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> element<span style="color: #339933;">,</span> i <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// does the same as above and will pass the &quot;instanceof&quot; check</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// also, note that the arguments are backwards</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// using 'this' here refers to the window object</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>If what you passed is not an instance of jQuery but it passed the isArray variable check, it means your call to the jQuery.map() utility function probably looked something like this:</p>

<div class="igBar"><span id="ljavascript-21"><a href="#" onclick="javascript:showPlainTxt('javascript-21'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-21">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><a href="/wp-content/themes/ljq/docs-1.7.php?fn=$.map"><span style="">$.<span style="color: #660066;">map</span></span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;color:#800000;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>In this case it falls back to this rigorous check to see if it's an array. 
<strong>Note:</strong> I'm breaking it down into separate lines so it's easier to understand.
</p>

<div class="igBar"><span id="ljavascript-22"><a href="#" onclick="javascript:showPlainTxt('javascript-22'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-22">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// make sure that we actually have a length property</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">length <span style="color: #339933;">!==</span> undefined</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// if it's a number, it could possibly be an array</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// but still needs some more checking</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &#038;& <span style="color: #000066; font-weight: bold;">typeof</span> length <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;number&quot;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &#038;& <span style="color: #009900;">&#40;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// make sure we're dealing with a set of non-empties</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span> length<span style="color: #339933;">&gt;</span> <span style="color: #CC0000;color:#800000;">0</span> &#038;& elems<span style="color: #009900;">&#91;</span> <span style="color: #CC0000;color:#800000;">0</span> <span style="color: #009900;">&#93;</span> &#038;& elems<span style="color: #009900;">&#91;</span> length <span style="color: #339933;">-</span><span style="color: #CC0000;color:#800000;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// if we're dealing with an empty node list - this is rare</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span> length <span style="color: #339933;">===</span> <span style="color: #CC0000;color:#800000;">0</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// this check is a last resort and only gets hit if we </span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// pass an array like $.map(new Array(1), fn);</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">||</span> jQuery.<span style="color: #660066;">isArray</span><span style="color: #009900;">&#40;</span> elems <span style="color: #009900;">&#41;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #009900;">&#41;</span></div></li>
</ol></div>
</div></div><br />

<p>The rest of the logic is pretty straight forward - just some for loops and super fun for-in loops. </p>

<p>Hope you enjoy jQuery.map() with object support!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2011/05/jquery-map-in-16/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>3 Quick Steps for a Painless Upgrade to jQuery 1.3</title>
		<link>http://www.learningjquery.com/2009/03/3-quick-steps-for-a-painless-upgrade-to-jquery-13</link>
		<comments>http://www.learningjquery.com/2009/03/3-quick-steps-for-a-painless-upgrade-to-jquery-13#comments</comments>
		<pubDate>Fri, 06 Mar 2009 05:58:09 +0000</pubDate>
		<dc:creator>Karl Swedberg</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[selectors]]></category>
		<category><![CDATA[upgrade]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=577</guid>
		<description><![CDATA[Since the release of jQuery 1.3 a month and a half ago, I've been keeping my eye on any troubles that people have had with their upgrades. Fortunately, most people have had no problems at all. For those who have, the issues have almost always been in one of three areas. Identifying these areas and [...]]]></description>
			<content:encoded><![CDATA[<p>Since the release of jQuery 1.3 a month and a half ago, I've been keeping my eye on any troubles that people have had with their upgrades. Fortunately, most people have had no problems at all. For those who have, the issues have almost always been in one of three areas. Identifying these areas and adjusting any legacy scripts ahead of time will go a long way toward ensuring a smooth transition to jQuery 1.3.x.</p>
<span id="more-577"></span>
[inline][/inline]
<h4>1. Update Attribute Selectors</h4>
<p>By far the most common stumbling block to the 1.3 upgrade has been the attribute selector. The XPath syntax for this selector &mdash; <code>[@attribute]</code> &mdash; has been <a href="http://blog.jquery.com/2007/08/24/jquery-114-faster-more-tests-ready-for-12/">deprecated since version 1.1.4</a> in August, 2007.  Still, many scripts, including some prominent plugins, continued to use the old syntax, which wasn't a problem until jQuery 1.3, at which point it was no longer supported. The correct syntax (since 1.1.4) follows the CSS standard: <code>[attribute]</code>.</p>
<p>The fix is quite simple. Simply search your scripts for <code>[@</code>. Go through the matches one by one, and if the string is being used as an attribute selector (and not, for example, in a regular expression), replace it with <code>[</code>. 
<h5>Removed XPath Syntax</h5>
</p><p>This will break your code in jQuery 1.3+:</p>

<div class="igBar"><span id="ljavascript-29"><a href="#" onclick="javascript:showPlainTxt('javascript-29'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-29">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[@href^=http]'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[@title=foo]'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />   

<h5>Current CSS Syntax</h5>  
<p>Use this instead:</p>
<div class="igBar"><span id="ljavascript-30"><a href="#" onclick="javascript:showPlainTxt('javascript-30'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-30">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a[href^=http]'</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'[title=foo]'</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<h4>2. Check Custom Selectors</h4>
<p>If you are using custom selectors, either in your own script or within a plugin, you might run across an error if the second argument's object value is a string: </p>

<div class="igBar"><span id="ljavascript-31"><a href="#" onclick="javascript:showPlainTxt('javascript-31'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-31">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>jQuery.<span style="color: #660066;">expr</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">':'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; hasSiblings<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jQuery(a).siblings(m[3]).length&gt;0&quot;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This is another easy fix. Simply change that value to an anonymous function and add what was in the string to the <code>return</code> statement:</p>
<div class="igBar"><span id="ljavascript-32"><a href="#" onclick="javascript:showPlainTxt('javascript-32'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-32">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>jQuery.<span style="color: #660066;">expr</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">':'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; hasSiblings<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>i<span style="color: #339933;">,</span>m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #000066; font-weight: bold;">return</span> jQuery<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=siblings"><span style="">siblings</span></a><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span><span style="color: #CC0000;color:#800000;">0</span>;<span style="color: #009900;">&#125;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />
<p>So, basically, you just change <code>"jQuery(a).siblings(m[3]).length>0"</code> to <code>function(a,i,m) {return <strong>jQuery(a).siblings(m[3]).length>0</strong>;}</code>.
</p>
<p>When someone on the <a href="http://groups.google.com/group/jquery-en/">jQuery Google Group</a> was having trouble with George Adamson's excellent <a href="http://www.softwareunity.com/jquery/JQueryMoreSelectors/">More Selectors plugin</a>, all I had to do to make it compatible with jQuery 1.3 was update the attribute selectors and change custom selectors (my updated script is <a href="http://plugins.learningjquery.com/moreselectors/">available here</a> temporarily until George has time to update the original).</p>
<h4>3. Note Visible and Hidden Elements</h4>
<p>This change didn't occur until jQuery 1.3.2, but if you are upgrading to the latest and greatest jQuery version, you'll want to be sure your scripts aren't relying on a quirk in how jQuery was determining the visibility of elements. Prior to 1.3.2, if you had an element with <code>display:block</code> inside a containing element with <code>display: none</code>, the inner element would still be reported as visible.
</p>

<div class="igBar"><span id="lhtml-33"><a href="#" onclick="javascript:showPlainTxt('html-33'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-33">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;outer1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inner1&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; you can't see me!</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Before version 1.3.2, <code>$('#inner'1).is(':visible')</code> would return <code>true</code> and <code>$('#inner1:hidden')</code> would match 0 elements. In order to find out if an element were truly hidden, meaning you couldn't see it, you'd have to check all of its ancestors to see if they were hidden as well. Remy Sharp put together a little custom selector to check if elements were <a href="remysharp.com/2008/10/17/jquery-really-visible/">*really* visible</a>.</p>

<p>With 1.3.2, <code>$('#inner').is(':visible)</code> returns <code>false</code> and <code>$('#inner:hidden')</code> match 1 element.</p>

<h5>Another Visible Wrinkle</h5>

<p>There is one more change in how visibility is detected in 1.3.2: elements with <code>visibility: hidden</code> are now considered visible, whereas before they were considered hidden.</p>

<div class="igBar"><span id="lhtml-34"><a href="#" onclick="javascript:showPlainTxt('html-34'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">HTML:</span><br /><div id="html-34">
<div class="html" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;outer2&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;inner2&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;visibility: hidden&quot;</span><span style="color: #000000;">&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp;you can't see me!</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;"><span style="color: #000000;">&lt;/div&gt;</span></span></div></li>
</ol></div>
</div></div><br />

<p>Given the above div structure, jQuery 1.3.2 considers #inner2 visible while prior versions considered it hidden.</p>
<p>Here is a quick demo, with two iFrames showing the differences in determining visibility:</p>

<iframe src="/examples/visibility-1.2.6.html" style="height:15.5em;width:260px;float:left; border-right: 1px solid #999; margin-right: 16px;"></iframe>
<iframe src="/examples/visibility-1.3.2.html" style="height:15.5em;width:300px;float:left"></iframe>

<p style="clear: left">I'm not sure how I feel about the new behavior with <code>visibility:hidden</code>, but I think the change was important for performance reasons.</p>
<h4>A Few Others</h4>
<p>There are a few other issues that may affect a small minority of users, but the three above represent the lion's share of reported problems that I've seen on the Google Groups. For more information about changes, see the <a href="http://docs.jquery.com/Release:jQuery_1.3#Upgrading">jQuery 1.3 Release Notes</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/03/3-quick-steps-for-a-painless-upgrade-to-jquery-13/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Implementing Prototype&#8217;s Array Methods in jQuery</title>
		<link>http://www.learningjquery.com/2009/02/implementing-prototypes-array-methods-in-jquery</link>
		<comments>http://www.learningjquery.com/2009/02/implementing-prototypes-array-methods-in-jquery#comments</comments>
		<pubDate>Sat, 21 Feb 2009 02:20:32 +0000</pubDate>
		<dc:creator>Josh Powell</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=450</guid>
		<description><![CDATA[One of the biggest concerns I've heard mentioned from users of the Prototype library about jQuery is the lack of support for various array methods. The robust features Prototype provides for arrays is of great benefit to developers that do a lot of array manipulation in their JavaScript. However, I find that after moving to [...]]]></description>
			<content:encoded><![CDATA[<p>One of the biggest concerns I've heard mentioned from users of the Prototype library about jQuery is the lack of support for various array methods.  The robust features Prototype provides for arrays is of great benefit to developers that do a lot of array manipulation in their JavaScript.  </p>

<p>However, I find that after moving to jQuery, I do less array manipulation than I had done with Prototype. Perhaps jQuery has altered my development pattern so I no longer need array manipulation, or perhaps I have shifted most of my data manipulation to the server. Whatever the case, I have only on occasion missed the Prototype array methods.</p>
 <span id="more-450"></span>
<p>While there is some overlap in the ways that jQuery and Prototype handle array manipulation, jQuery does a few things Prototype doesn't do, and Prototype does a number of things that jQuery doesn't. I began writing an article about these differences, but soon got side-tracked writing a jQuery plugin to mimic the array methods Prototype provides.</p>
[inline][/inline]
<p>You can find the plugin at <a href="http://code.google.com/p/jquery-protify-js/">http://code.google.com/p/jquery-protify-js/</a></p>
 
<p>With this plugin, you can give a particular array all of the methods that Prototype adds to their <code>Array</code> and <code>Enumerable</code> objects. You can use the methods two different ways. The first does not extend the original array:</p>
 
<div class="igBar"><span id="ljavascript-46"><a href="#" onclick="javascript:showPlainTxt('javascript-46'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-46">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> protArray <span style="color: #339933;">=</span> $.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>This will return an array extended with the Prototype library's methods, but leave the original array untouched.</p> 

<p>The second extends the original array by passing in <code>true</code> as the second parameter:</p>
 
<div class="igBar"><span id="ljavascript-47"><a href="#" onclick="javascript:showPlainTxt('javascript-47'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-47">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>In either case, the JavaScript Array prototype is untouched. When you create new arrays, it will not have the new methods. This way of writing code adds the new methods when they are needed while leaving the underlying JavaScript prototypes untouched. The return value of the methods is an array with the extended methods so that they can be chained.</p>
 
<div class="igBar"><span id="ljavascript-48"><a href="#" onclick="javascript:showPlainTxt('javascript-48'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-48">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr2 <span style="color: #339933;">=</span> $.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">compact</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #660066;">findAll</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">&gt;=</span><span style="color: #CC0000;color:#800000;">3</span>; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<a href="/wp-content/themes/ljq/docs-1.7.php?fn=first"><span style="">first</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// 3</span></div></li>
</ol></div>
</div></div><br />
              
<p>The above code first returns the <code>arr</code> array values in a new array with the Prototype methods, removes any <code>null</code>/<code>undefined</code> values (using <code>compact</code>), finds all the values in the array greater than or equal to 3 and returns an extended array of them (using <code>findAll</code>), and finally returns the first value of that array (using <code>first</code>).  It does all of this without touching the prototype for all arrays.</p>
 
<p>Here are some useful Prototype array methods included in the plugin:</p>

<h4>all()</h4>
<p>returns <code>true</code> if every member of the array <code>== true</code> and returns <code>false</code> if even one member <code>!= true</code>.</p>

<div class="igBar"><span id="ljavascript-49"><a href="#" onclick="javascript:showPlainTxt('javascript-49'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-49">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">all</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// true</span></div></li>
</ol></div>
</div></div><br />

<h4>any()</h4>
<p>returns true if even one member of the array <code>== true</code> and returns false if every member <code>!= true</code>.</p>

<div class="igBar"><span id="ljavascript-50"><a href="#" onclick="javascript:showPlainTxt('javascript-50'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-50">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> $.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">any</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// true</span></div></li>
</ol></div>
</div></div><br />

<h4>map()</h4> 
<p>similiar to jQuery's <code>$.map</code>.  It executes the function passed in on every of member of the array and returns an array of the results.</p>

<div class="igBar"><span id="ljavascript-51"><a href="#" onclick="javascript:showPlainTxt('javascript-51'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-51">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=map"><span style="">map</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> n <span style="color: #339933;">*</span> <span style="color: #CC0000;color:#800000;">2</span>; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// &nbsp;[2,4,6,8,10,12]</span></div></li>
</ol></div>
</div></div><br />

<h4>eachSlice(number)</h4> 
<p>slices the array into an array of arrays of the size passed in.  There is an optional second parameter which is a function that acts on each array partition before placing it in the slice.</p>

<div class="igBar"><span id="ljavascript-52"><a href="#" onclick="javascript:showPlainTxt('javascript-52'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-52">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">eachSlice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #009900;">&#41;</span>; &nbsp;<span style="color: #006600; font-style: italic;">// [[1,2],[3,4],[5,6]]</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">eachSlice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> n.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=map"><span style="">map</span></a><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;color:#800000;">2</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// [[2,4],[6,8],[10,12]]</span></div></li>
</ol></div>
</div></div><br />

<h4>include(value)</h4>
<p>returns <code>true</code> if the value is in the array (using == equality).</p>

<div class="igBar"><span id="ljavascript-53"><a href="#" onclick="javascript:showPlainTxt('javascript-53'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-53">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// true</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">arr.<span style="color: #660066;">include</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'pie'</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// false</span></div></li>
</ol></div>
</div></div><br />

<h4>max()/min()</h4> 

<p>returns the maximum/minimum value of the array.  Optional parameter is a function that can be used to define the comparison and what gets returned.</p>

<div class="igBar"><span id="ljavascript-54"><a href="#" onclick="javascript:showPlainTxt('javascript-54'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-54">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'frank'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'joe'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">12</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; .<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> person.<span style="color: #660066;">age</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// returns 12</span></div></li>
</ol></div>
</div></div><br />

<h4>partition()</h4>
<p>returns an array with two arrays in it.  The first array contains the values that <code>== true</code> in the initial array, and the second all of the values <code>!= true</code>. There is an optional parameter that is a function used to do the evaluation.</p>

<div class="igBar"><span id="ljavascript-55"><a href="#" onclick="javascript:showPlainTxt('javascript-55'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-55">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;color:#800000;">6</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">partition</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp; <span style="color: #000066; font-weight: bold;">return</span> n <span style="">&lt;</span><span style="color: #339933;">=</span> <span style="color: #CC0000;color:#800000;">3</span>; </div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// [[1,2,3],[4,5,6]]</span></div></li>
</ol></div>
</div></div><br />
<br />
<h4>pluck()</h4>

<p>And finally, my favorite.  The <code>pluck()</code> method iterates through every member of the array and returns an array of the value of the attribute name passed in.</p>

<div class="igBar"><span id="ljavascript-56"><a href="#" onclick="javascript:showPlainTxt('javascript-56'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-56">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Frank'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">10</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Joe'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'age'</span><span style="color: #339933;">:</span> <span style="color: #CC0000;color:#800000;">12</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluck</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'age'</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// [10, 12]</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.<span style="color: #660066;">protify</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pluck</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// ['Frank, 'Joe']</span></div></li>
</ol></div>
</div></div><br />

<p>For a full listing and description of all the methods, see the Prototype documentation on arrays and enumerables.</p>

<p>Arrays: <a href="http://prototypejs.org/api/array">http://prototypejs.org/api/array</a></p>

<p>Enumerables: <a href="http://prototypejs.org/api/enumerable">http://prototypejs.org/api/enumerable</a></p>

<p>The <code>toJSON</code> method has not yet been implemented because it depends on object methods that Prototype provides.  Can you guess what my next project is?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2009/02/implementing-prototypes-array-methods-in-jquery/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Peeling Away the jQuery Wrapper and Finding an Array</title>
		<link>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper</link>
		<comments>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper#comments</comments>
		<pubDate>Mon, 22 Dec 2008 16:46:56 +0000</pubDate>
		<dc:creator>Cody Lindley</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[DOM Traversing]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[jQuery Resources]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[objects]]></category>

		<guid isPermaLink="false">http://www.learningjquery.com/?p=196</guid>
		<description><![CDATA[If you haven't poked around under the hood of jQuery, you might not be aware that when you pass the jQuery function an expression or DOM element it places these elements (or, possibly a single element) into an object, and then this object is returned so that it can be chained. Without getting into the [...]]]></description>
			<content:encoded><![CDATA[<p>If you haven't poked around under the hood of jQuery, you might not be aware that when you pass the jQuery function an expression or DOM element it places these elements (or, possibly a single element) into an object, and then this object is returned so that it can be chained. Without getting into the details of chaining, the fundamental concept to remember is this:</p>

<p><em>Most jQuery methods return the jQuery object itself, which allows methods to be chained.</em></p>
<span id="more-196"></span>
<p>The interesting thing about the jQuery object is that while its datatype is an object, it has array-like characteristics:</p>
<ul>
	<li>its property names (the ones that refer to DOM elements, at least) are numeric</li>
	<li>it has a length property</li>
</ul>
<p>Have you ever noticed, for example, a snippet of jQuery code that directly accesses a DOM element by using the bracket operator with the jQuery function?</p>

<div class="igBar"><span id="ljavascript-64"><a href="#" onclick="javascript:showPlainTxt('javascript-64'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-64">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>; <span style="color: #006600; font-style: italic;">// give me the first DOM element that the</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">// jQuery function found</span></div></li>
</ol></div>
</div></div><br />

<p>Now, the jQuery function returns an object, but it looks like we just treated it as an array. It can also be written like this:</p>

<div class="igBar"><span id="ljavascript-65"><a href="#" onclick="javascript:showPlainTxt('javascript-65'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-65">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// do the same, using a jQuery method</span></div></li>
</ol></div>
</div></div><br />

<p>If this is confusing, let's see if we can clear it up with some code. Go to the jQuery.com homepage, open up <a href="http://getfirebug.com/">Firebug</a> using Firefox, and run the following line of code in the console.</p>

<div class="igBar"><span id="ljavascript-66"><a href="#" onclick="javascript:showPlainTxt('javascript-66'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-66">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>; <span style="color: #006600; font-style: italic;">// toSource() is a method of all JavaScript objects</span></div></li>
</ol></div>
</div></div><br />

<p>You should be looking at this in the console:</p>

<code>"({length:25, 0:{}, 1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{}, 7:{}, 8:{}, 9:{}, 10:{}, 11:{}, 12:{}, 13:{}, 14:{}, 15:{}, 16:{}, 17:{}, 18:{}, 19:{}, 20:{}, 21:{}, 22:{}, 23:{}, 24:{}, prevObject:{0:{}, length:1}})"</code>

<p>Looky there! The jQuery function is returning an object with a bunch of properties. If we tack on the bracket operator to the jQuery function, which returns an object, we can access the properties of that object because inside the object are properties with a numeric property name. Fascinating isn't it?</p>

<p>I know what you're thinking: this tutorial is about arrays, not objects. Right, so let's get to the array part. Since we now know that the jQuery function returns an object that only masquerades as a JavaScript array the question at hand is how do I get a true JavaScript array from a jQuery wrapper object. This is actually very simple. Most of you might not have been aware (if not, I showed you earlier) that <code>$('div')[0]</code> and <code>$('div').get(0)</code> return a reference to the first element in the jQuery wrapper object. But, the <code>get()</code> method actually has another purpose. It can quickly turn the DOM elements found in the jQuery wrapper into an array of DOM elements.</p>

<p>Let's return to jQuery.com and open up Firebug again. Inside the console run the following line of code.</p>

<div class="igBar"><span id="ljavascript-67"><a href="#" onclick="javascript:showPlainTxt('javascript-67'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-67">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</div></li>
</ol></div>
</div></div><br />

<p>You should now be looking at this in the firebug console:</p>

<p><code>"[{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]"</code></p>

<p>Essentially the <code>.get()</code> method placed all of the DOM elements inside the jQuery wrapper into an array and returned that array. So now you can use default array methods on your array of elements. For example:</p>

<div class="igBar"><span id="ljavascript-68"><a href="#" onclick="javascript:showPlainTxt('javascript-68'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-68">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span>; <span style="color: #006600; font-style: italic;">//returns the array as a string</span></div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;"><span style="color: #006600; font-style: italic;">//this is a method of the array object</span></div></li>
</ol></div>
</div></div><br />

<p>Additionally, since we now have an array we can write a statement like this:</p>

<div class="igBar"><span id="ljavascript-69"><a href="#" onclick="javascript:showPlainTxt('javascript-69'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-69">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<a href="/wp-content/themes/ljq/docs-1.7.php?fn=get"><span style="">get</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;color:#800000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>; <span style="color: #006600; font-style: italic;">//returns the first div's id attribute value</span></div></li>
</ol></div>
</div></div><br />

<p>There is something worth noting here that might not be obvious to those starting out with jQuery or JavaScript. Since we are now returning an array, the chain is broken. You can't chain any jQuery methods after using the <code>.get()</code> method.</p>

<p>But, heck, you have an array. Now you can use the utility functions that jQuery provides (<code>$.each()</code>, <code>$.grep()</code>, <code>$.map()</code>, <code>$.inArray()</code>, <code>$.unique()</code>), as well as core JavaScript methods and properties, to work on the array. I'm not going to demonstrate all of the jQuery utility functions that can be used on an array, but I do want to examine one of them here in this tutorial.</p>

<p>The <code>$.each()</code> utility function, not to be confused with the <code>$('selector').each()</code> method used to operate on a set of DOM elements in a jQuery wrapper, can be used to seamlessly iterate over an array or object. For example, let's say that you would like to store (or cache) a set of <code>&lt;li&gt;</code> elements in an array, because maybe you are going to remove them from the DOM temporarily but eventually will be placing them back on the page. To do this you could use the <code>$.each()</code> utility function to loop over an array and append each <code>&lt;li&gt;</code> element back to the page. In the code example below this is done on page load by converting and storing the jQuery wrapper of <code>&lt;li&gt;</code> elements to an array, and then looping over this array in order to place them back on the page.</p>

<div class="igBar"><span id="ljavascript-70"><a href="#" onclick="javascript:showPlainTxt('javascript-70'); return false;">PLAIN TEXT</a></span></div><div class="syntax_hilite"><span class="langName">JavaScript:</span><br /><div id="javascript-70">
<div class="javascript" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;html&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;head&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js&quot;&gt;&lt;/script&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/head&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;body&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;ul&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 1&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 2&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 3&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 4&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 5&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 6&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 7&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;li&gt;task 8&lt;/li&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/ul&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;script&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">var arrayList = $('ul li').get();</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$('ul').empty();</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$.each(arrayList, function(){</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">$('ul').append('&lt;li&gt;&lt;del&gt;'+$(this).html() +'&lt;/del&gt; complete&lt;/li&gt;');</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">});</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&nbsp;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/script&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/body&gt;</div></li>
<li style="font-weight: normal; vertical-align:top;color:#ACAA9A;"><div style="background-color: transparent;">&lt;/html&gt;</div></li>
</ol></div>
</div></div><br />

<p>Notice that I am forgoing the use of the <code>.ready()</code> event by simply placing my jQuery code before the closing body element.</p>

<p>So, hopefully it's obvious how extremely handy it can be at times to make an array out of a jQuery wrapper. In fact, once you are dealing with an array, you can use a handy plugin called <a href="http://plugins.jquery.com/project/rich-array">Rich Array</a>. This plugin provides the following additional utility functions for arrays. And yes, there is a bit of duplication here with some of the core utility functions.</p>

<p>
<strong>$.richArray.in()</strong> //Checks whether an array contains some value<br />
<strong>$.richArray.unique()</strong> //Produces the duplicate-free version of the array<br />
<strong>$.richArray.diff()</strong> //Finds the difference between two arrays.<br />
<strong>$.richArray.intersect()</strong> //Finds the intersection of two arrays<br />
<strong>$.richArray.filter()</strong> //Applies filter to the array, using callback function<br />
<strong>$.richArray.map()</strong> //Applies callback function for each element in the input array, and returns array of values that this function returned<br />
<strong>$.richArray.sum()</strong> //Computes the sum of all array elements<br />
<strong>$.richArray.product()</strong> //Calculates the production of all elements of the array<br />
<strong>$.richArray.reduce()</strong> //Reduces the array. One-element arrays are turned into their unique element<br />
<strong>$.richArray.compact()</strong> //Creates new version of array without null/undefined values<br />
<strong>$.richArray.without()</strong> //Creates a new version of the array that doesn't contain the specified value</p>

[inline][/inline]]]></content:encoded>
			<wfw:commentRss>http://www.learningjquery.com/2008/12/peeling-away-the-jquery-wrapper/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced (User agent is rejected)
Database Caching 7/26 queries in 0.007 seconds using disk: basic
Object Caching 396/438 objects using disk: basic
Content Delivery Network via learningjquery.kswedberg.netdna-cdn.com

Served from: www.learningjquery.com @ 2012-02-08 13:27:40 -->
