<?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>adam j. sontag</title>
	<atom:link href="http://ajpiano.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ajpiano.com</link>
	<description>this is like a website, only less finished.</description>
	<lastBuildDate>Thu, 02 Sep 2010 23:51:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title></title>
		<link>http://ajpiano.com/2010/when-to-use-jquerys-map-methods/</link>
		<comments>http://ajpiano.com/2010/when-to-use-jquerys-map-methods/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 23:34:22 +0000</pubDate>
		<dc:creator>ajpiano</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ajpiano.com/?p=25</guid>
		<description><![CDATA[Have you ever written a bit of jQuery code that looks like this? var pee = []; $(".poo").each(function() { pee.push(this.id); }); pee.join(); Or even worse, this? var pee = "", poo = $(".poo"); poo.each(function(i,elem) { pee += this.id; if (i &#60; poo.length) { pee += ","; } }); If you are are doing Array.push or [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever written a bit of jQuery code that looks like this?</p>
<pre class="js" name="code">var pee = [];
$(".poo").each(function() {
pee.push(this.id);
});
pee.join();</pre>
<p>Or even worse, this?</p>
<pre class="js" name="code">var pee = "", poo = $(".poo");
poo.each(function(i,elem) {
  pee += this.id;
  if (i &lt; poo.length) {
    pee += ",";
  }
});</pre>
<p>If you are are doing Array.push or string concatenation inside of a loop, then what you want to be doing is using jQuery&#8217;s <a href="http://api.jquery.com/map/">map</a> <a href="http://api.jquery.com/jQuery.map/">methods</a>, which exist for the explicit purpose of creating a new array of values based on an existing set, like so:</p>
<pre class="js" name="code">var pee = $(".poo").map(function() {
 return this.id;
}).get().join();</pre>
<p>There are a few caveats, however.  Let&#8217;s start with that <a href="http://api.jquery.com/get">.get()</a> at the end.</p>
<p>When you use jQuery.fn.map, the result is a jQuery object &#8211; an array of values with a set of functions made available by jQuery.  Usually the array is compromised of DOM nodes, but after using $.fn.map, it&#8217;s just an array of arbitrary values.  Most people are familiar with using <a href="http://api.jquery.com/get">.get(index)</a> to access the DOM node at a selected index, but when .get() is called with no arguments, it returns the entire underlying array.  This means you can&#8217;t call jQuery methods on it anymore, but you can call Array.join, or any other Array method, which was probably the reason you wanted an array to begin with.  So go out and .get() it! </p>
<h3>jQuery.fn.map vs jQuery.map</h3>
<p>Just like jQuery provides an iterator for jQuery objects (<a href="http://api.jquery.com/each/">jQuery.fn.each</a>) and a &#8220;generic&#8221; iterator for any array or object (<a href="http://api.jquery.com/jQuery.each">jQuery.each</a>), there are also two flavours of .map.  <a href="http://api.jquery.com/map/">jQuery.fn.map</a>, illustrated above, works on actual jQuery objects, while <a href="http://api.jquery.com/jQuery.map/">jQuery.map </a>works on vanilla Arrays &#8211; and ONLY arrays.  As of this writing, you can&#8217;t use jQuery.map on a plain Object.  I don&#8217;t know why, but that&#8217;s just how it is, kid.  Additionally, jQuery.map returns an array, not a jQuery object, so there is no need to call .get() after using it. </p>
<p>Also, for some bizarre, cruel reason, the arguments to the function supplied jQuery.fn.map and jQuery.map are <strong>NOT</strong> consistent with each other.  The function signature for effin&#8217; map, the one on the jQuery prototype, is <code>function(index,element)</code>, consistent with other iterating functions on the jQuery prototpye, whereas the generic jQuery.map&#8217;s signature is <code>function(element,index)</code>, because of [REDACTED].</p>
<p>When might you use jQuery.map?  Consider this <del>stupid</del> useful acronymify example:</p>
<pre class="js" name="code">function acronymify(str) {
    return jQuery.map(str.split(" "),function(s) {
        return s.charAt(0);
    }).join("");
}
acronymify("Cash Rules Everything Around Me");
// --> "CREAM"</pre>
<p>So, to answer the oft-asked question, &#8220;How do I know whether to use .each() or .map()?&#8221; If you are iterating over a set for the purpose of creating a new array or string based on the values in that set, use .map().  If you are iterating just because you need to *do* something to each item in a set, or because you want to create an object, use .each().</p>
<p>PS. We talked about this at length in <a href="http://yayquery.com/#ep13">episode 13 of yayQuery!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ajpiano.com/2010/when-to-use-jquerys-map-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The opposite of jQuery&#8217;s .is() method is not .not(), it is !.is()</title>
		<link>http://ajpiano.com/2010/the-opposite-of-jquerys-is-method-is-not-not-it-is-is/</link>
		<comments>http://ajpiano.com/2010/the-opposite-of-jquerys-is-method-is-not-not-it-is-is/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 19:27:42 +0000</pubDate>
		<dc:creator>ajpiano</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://ajpiano.com/?p=7</guid>
		<description><![CDATA[FAR too often I have seen people who are new to jQuery make the same irritating mistake, which I want to address once and for all.  jQuery has an .is() method, which checks to see if at least one element in the current jQuery selection matches a selector, and returns a boolean reflecting this fact. [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">FAR too often I have seen people who are new to jQuery make the same irritating mistake, which I want to address once and for all.  jQuery has an <em><a title=".is()" href="http://api.jquery.com/is" target="_blank">.is()</a> </em>method, which checks to see if at least one element in the current jQuery selection matches a selector, and returns a boolean reflecting this fact.  For instance:</div>
<pre class="js" name="code">if ($("#someCheckbox").is(":checked")) {
//do something if the checkbox is checked
}
if ($("div').is("[data-pants]")) {
// do something if at least one div has
// a data-attribute called 'data-pants'
}</pre>
<div>This is all well and good, and very convenient.  While some purists might object to the use of this method (because of &#8220;overhead&#8221;) compared to an explicit check on, well, whatever it is you&#8217;re trying to check, the simplicity of <em>.is()</em> is seductive. However, its existence leads a lot of people to make the same very bad assumption about jQuery&#8217;s <em><a title=".not()" href="http://api.jquery.com/not" target="_blank">.not()</a></em> method.</div>
<pre class="js" name="code">// WHY DOESN'T THIS WORKS?!?
// IT'S ALWAYS SAY TRUE!  WHY?!
if ($("#someCheckbox").not(":checked")) {
// do something if the checkbox is NOT checked
}</pre>
<div>WHY?!? You ask?  Because jQuery&#8217;s <em>.not()</em> method is a *filtering* method, it does not return a boolean value.  <em>.not() </em>is, in fact, the opposite of <em><a title=".filter()" href="http://api.jquery.com/filter" target="_blank">.filter()</a></em>, checking each element in the matched set and returning only those that do not match the selector.  Given the following DOM:</div>
<pre class="xml" name="code">&lt;p&gt;Hello&lt;/p&gt;
&lt;p&gt;There&lt;/p&gt;
&lt;p class="greeted"&gt;Sirs&lt;/p&gt;
&lt;p&gt;Have a great day!&lt;/p&gt;</pre>
<pre class="js" name='code'>var paragrafz = $("p");
paragrafz.length == 4;
paragrafz.filter(".greeted").length == 1
paragrafz.not(".greeted").length == 3;</pre>
<div>Since <em>.not()</em> is a filtering method, it returns a jQuery object, which, like any object ever anywhere always, will return true when tested for its boolean-ness.  Thus, the <em>.not()</em> method simply is not appropriate for doing checks such as the one above.  Luckily, JavaScript provides us with a <strong> not operator (!)</strong>, which is <strong>the right way</strong> to test for the opposite of <em>.is()</em>. For instance,</div>
<pre class="js" name="code">if (!$("#someCheckbox").is(":checked")) {
// do something if the checkbox is NOT checked
}</pre>
<div>While this seems incredibly obvious to many, I have seen this errant assumption far too many times to remain silent any longer!  So say it loudly, proudly with me now: <strong>The opposite of jQuery&#8217;s .is() method is not .<em>not(</em>), it is <em>!.is()</em></strong></div>
]]></content:encoded>
			<wfw:commentRss>http://ajpiano.com/2010/the-opposite-of-jquerys-is-method-is-not-not-it-is-is/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
