<?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>Web News &#187; map</title>
	<atom:link href="http://gogo.org.ua/tag/map/feed" rel="self" type="application/rss+xml" />
	<link>http://gogo.org.ua</link>
	<description></description>
	<lastBuildDate>Tue, 08 Dec 2009 06:00:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>API: map &amp; grep</title>
		<link>http://gogo.org.ua/api-map-grep</link>
		<comments>http://gogo.org.ua/api-map-grep#comments</comments>
		<pubDate>Tue, 31 Mar 2009 15:36:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[map]]></category>

		<guid isPermaLink="false">http://gogo.org.ua/api-map-grep</guid>
		<description><![CDATA[API: map &#38; grep
&#160;
map and grep are two utilities that are extremely useful if you know how to use them, but more often that not, I see more convoluted code written to achieve the effect of these functions.
&#160;
Historically, I believe that John Resig was inspired by the Perl functions of the same name &#8211; which [...]]]></description>
			<content:encoded><![CDATA[<p align="center"><font size="5"><strong><font face="trebuchet ms,geneva">API: map &amp; grep</font></strong></font></p>
<p class="meta" align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="3"><span class="pln">map</span> and <span class="pln">grep</span> are two utilities that are extremely useful if you know how to use them, but more often that not, I see more convoluted code written to achieve the effect of these functions.</font></p>
<p align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="3">Historically, I <em>believe</em> that John Resig was inspired by the Perl functions of the same name &#8211; which are basic building blocks of the Perl programming language. Let&rsquo;s look at how you can use these functions.</font></p>
<p align="right"><a href="http://valant.com.ua" target="_blank" title="valant"><font face="trebuchet ms,geneva" size="3">valant</font></a></p>
<p align="justify"><span id="more-79"></span></p>
<p align="justify">&nbsp;</p>
<h2 align="justify"><font face="trebuchet ms,geneva" size="3">Map</font></h2>
<p align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="3">Map comes in two flavours: <span class="pln">$</span><span class="pun">.</span><span class="pln">map</span><span class="pun">(</span><span class="pln">array</span><span class="pun">,</span><span class="pln"> callback</span><span class="pun">)</span> and <span class="pln">$</span><span class="pun">(</span><span class="str">&#8216;element&#8217;</span><span class="pun">).</span><span class="pln">map</span><span class="pun">(</span><span class="pln">callback</span><span class="pun">)</span>.  The first allows you to iterate vanilla arrays, and the second to loop through elements.</font></p>
<p align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="3">The <span class="pln">$</span><span class="pun">.</span><span class="pln">map</span> function returns a plain array, where as <span class="pln">$</span><span class="pun">(</span><span class="str">&#8216;element&#8217;</span><span class="pun">).</span><span class="pln">map</span> returns a jQuery wrapped array, which means you still have access to functions like <span class="pln">each</span>.</font></p>
<p align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="3">A really powerful use of <span class="pln">map</span> to pull out a comma separated list of element IDs from a list of anchor element, as often explained by ajpiano on the #jquery IRC channel:</font></p>
<p align="justify">&nbsp;</p>
<pre><font face="trebuchet ms,geneva" size="3"><span class="kwd">var</span><span class="pln"> panelIds </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'a'</span><span class="pun">).</span><span class="pln">map</span><span class="pun">(</span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  &nbsp; </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">hash</span><span class="pun">;</span><span class="pln">  </span><span class="pun">}).</span><span class="pln">get</span><span class="pun">().</span><span class="pln">join</span><span class="pun">(</span><span class="str">&quot;,&quot;</span><span class="pun">)</span><span class="pln">  </span></font></pre>
<pre><font face="trebuchet ms,geneva" size="3"><span class="com">// returns: #tabA,#tabB,#tabC</span></font></pre>
<p align="justify"><font face="trebuchet ms,geneva" size="3">This can be useful to collect the list of tabs the links are related to as seen in the jQuery look: Tim Van Damme tutorial last month.</font></p>
<p align="justify">&nbsp;</p>
<h2 align="justify"><font face="trebuchet ms,geneva" size="3">Grep</font></h2>
<p align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="3"><span class="pln">grep</span> is similar to <span class="pln">$</span><span class="pun">.</span><span class="pln">map</span> in that it will return an array.  It&rsquo;s only available in the <span class="pln">$</span><span class="pun">.</span><span class="pln">grep</span></font><font size="3"> flavour, and allows you to create new arrays when a certain condition is met (which is defined in the callback).</font></p>
<p align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="3">Note that in the <span class="kwd">this</span> keyword is a reference to the <span class="pln">window</span> object, rather than the current item as you might expect.</font></p>
<p align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="3">A contrived example could be to create a comma separated list of IDs that start with the word &lsquo;tab&rsquo;:</font></p>
<p align="justify">&nbsp;</p>
<pre><font face="trebuchet ms,geneva" size="3"><span class="kwd">var</span><span class="pln"> g </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">grep</span><span class="pun">(</span><span class="pln">$</span><span class="pun">(</span><span class="str">'a'</span><span class="pun">).</span><span class="pln">get</span><span class="pun">(),</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> </span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> &nbsp; </span></font></pre>
<pre><font face="trebuchet ms,geneva" size="3"><span class="pln">&nbsp; </span><span class="kwd">return</span><span class="pln"> </span><span class="pun">/^</span><span class="com">#tab/.test(item.hash);</span><span class="pln"> </span><span class="pun">});</span><span class="pln">  </span><span class="kwd">var</span><span class="pln"> panelIds </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">map</span><span class="pun">(</span><span class="pln">g</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> </span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></font></pre>
<pre><font face="trebuchet ms,geneva" size="3"><span class="pln"> &nbsp; &nbsp; </span><span class="kwd">return</span><span class="pln"> item</span><span class="pun">.</span><span class="pln">hash </span><span class="pun">}).</span><span class="pln">join</span><span class="pun">(</span><span class="str">','</span><span class="pun">);</span></font></pre>
<p align="justify">  <font face="trebuchet ms,geneva" size="3"><br /> These are pretty useful utilities for spe</font><font size="3">cific problem.</font></p>
<p align="justify">&nbsp;</p>
<p align="justify"><font face="trebuchet ms,geneva" size="4">&nbsp;<a href="http://valant.com.ua" target="_blank" title="valant">Valant</a> uses actively jQuery in developming new features.</font> </p>
<a href='javascript: void(0);' onclick="window.open('http://gogo.org.ua/wp-content/plugins/email_post/email_post_process_link.php?&email_post_link_id=79','popup_mailform',
    'toolbar=0,status=0,menubar=0,scrollbars=1,resizable=0,width=630,height=600, top=0, left=0')"><img src='http://gogo.org.ua//wp-content/plugins/email_post/email_post.gif' style='border: 0px none;' /></a>&nbsp;<a href='javascript: void(0);' onclick="window.open('http://gogo.org.ua/wp-content/plugins/email_post/email_post_process_link.php?&email_post_link_id=79','popup_mailform',
    'toolbar=0,status=0,menubar=0,scrollbars=1,resizable=0,width=630,height=600, top=0, left=0')">Mail this post</a>]]></content:encoded>
			<wfw:commentRss>http://gogo.org.ua/api-map-grep/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
