<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
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/"
> <channel><title>Comments on: How To Emulate a Foreach Loop in JavaScript</title> <atom:link href="http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/feed/" rel="self" type="application/rss+xml" /><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/</link> <description>Web Development Tutorials, Tips &#38; Techniques</description> <lastBuildDate>Tue, 07 Sep 2010 15:47:28 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>By: Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; Build a Bare-Bones WordPress 2.8+ Widget</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11199</link> <dc:creator>Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; Build a Bare-Bones WordPress 2.8+ Widget</dc:creator> <pubDate>Sun, 07 Feb 2010 07:14:12 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11199</guid> <description>[...] How To Emulate a Foreach Loop in JavaScript [...]</description> <content:encoded><![CDATA[<p>[...] How To Emulate a Foreach Loop in JavaScript [...]</p> ]]></content:encoded> </item> <item><title>By: Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; How To Create Simple, Stylish and Swappable Image Captions</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11196</link> <dc:creator>Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; How To Create Simple, Stylish and Swappable Image Captions</dc:creator> <pubDate>Sun, 07 Feb 2010 04:32:15 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11196</guid> <description>[...] How To Emulate a Foreach Loop in JavaScript [...]</description> <content:encoded><![CDATA[<p>[...] How To Emulate a Foreach Loop in JavaScript [...]</p> ]]></content:encoded> </item> <item><title>By: Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; Running jQuery with Other Frameworks Via noConflict</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11190</link> <dc:creator>Saint John Web Design &#124; Informative Computer Solutions &#187; Blog Archive &#187; Running jQuery with Other Frameworks Via noConflict</dc:creator> <pubDate>Sun, 07 Feb 2010 04:27:44 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11190</guid> <description>[...] How To Emulate a Foreach Loop in JavaScript [...]</description> <content:encoded><![CDATA[<p>[...] How To Emulate a Foreach Loop in JavaScript [...]</p> ]]></content:encoded> </item> <item><title>By: Mike Capson &#187; Blog Archive &#187; How To Create Simple, Stylish and Swappable Image Captions</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11181</link> <dc:creator>Mike Capson &#187; Blog Archive &#187; How To Create Simple, Stylish and Swappable Image Captions</dc:creator> <pubDate>Sat, 06 Feb 2010 00:59:47 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11181</guid> <description>[...] How To Emulate a Foreach Loop in JavaScript [...]</description> <content:encoded><![CDATA[<p>[...] How To Emulate a Foreach Loop in JavaScript [...]</p> ]]></content:encoded> </item> <item><title>By: Lee Kowalkowski</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-11113</link> <dc:creator>Lee Kowalkowski</dc:creator> <pubDate>Wed, 03 Feb 2010 13:29:47 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-11113</guid> <description>You only need to cache the array length if the array is actually a DOM collection (e.g. element.childNodes).  JS for(... in ...) won&#039;t work cross browser over DOM collections anyway - because some browsers include the length property and will use that as one of the iterations.You need for(... in ...) to iterate through properties of an object though, therefore I&#039;d never advise modifying Array.prototype.  E.g:var a = [];
a[&quot;key1&quot;] = &quot;value1&quot;;
a[&quot;key2&quot;] = &quot;value2&quot;;.. if Array.prototype has been modified, it makes things more difficult.</description> <content:encoded><![CDATA[<p>You only need to cache the array length if the array is actually a DOM collection (e.g. element.childNodes).  JS for(&#8230; in &#8230;) won&#8217;t work cross browser over DOM collections anyway &#8211; because some browsers include the length property and will use that as one of the iterations.</p><p>You need for(&#8230; in &#8230;) to iterate through properties of an object though, therefore I&#8217;d never advise modifying Array.prototype.  E.g:</p><p>var a = [];<br
/> a["key1"] = &#8220;value1&#8243;;<br
/> a["key2"] = &#8220;value2&#8243;;</p><p>.. if Array.prototype has been modified, it makes things more difficult.</p> ]]></content:encoded> </item> <item><title>By: Rob Glazebrook</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-10762</link> <dc:creator>Rob Glazebrook</dc:creator> <pubDate>Mon, 25 Jan 2010 15:27:15 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-10762</guid> <description>Hi Frank,My only concern with Brandon&#039;s example is that if you ~do~ happen to be using a library like jQuery (and that&#039;s a growing percentage of the web populace), that structure can give you problems. I tested it for the purposes of this article and array[i] was returning a function, not the array string I was expecting.So, if you&#039;re not using jQuery or a similar framework, and never plan to use a JavaScript framework on your pages, by all means go with that system. But if you&#039;re already using jQuery or another framework for something else, you may run into problems!</description> <content:encoded><![CDATA[<p>Hi Frank,</p><p>My only concern with Brandon&#8217;s example is that if you ~do~ happen to be using a library like jQuery (and that&#8217;s a growing percentage of the web populace), that structure can give you problems. I tested it for the purposes of this article and array[i] was returning a function, not the array string I was expecting.</p><p>So, if you&#8217;re not using jQuery or a similar framework, and never plan to use a JavaScript framework on your pages, by all means go with that system. But if you&#8217;re already using jQuery or another framework for something else, you may run into problems!</p> ]]></content:encoded> </item> <item><title>By: Frank Schenk</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-10706</link> <dc:creator>Frank Schenk</dc:creator> <pubDate>Sun, 24 Jan 2010 20:09:41 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-10706</guid> <description>What&#039;s wrong with Brandons example? It works perfectly and it doesn&#039;t require a bloated library like jquery.for() is btw much faster than foreach in php, if the maximum number is given as variable, not as function, thats the problem with foreach, as it always has to check the content of the given array.so:
$max = count( $array);
for( $i=0; $&lt;$max; $i++){
$array[$i] = &#039;Do whatever you want to do&#039;;
}is way faster than foreach.cheers, Frank</description> <content:encoded><![CDATA[<p>What&#8217;s wrong with Brandons example? It works perfectly and it doesn&#8217;t require a bloated library like jquery.</p><p>for() is btw much faster than foreach in php, if the maximum number is given as variable, not as function, thats the problem with foreach, as it always has to check the content of the given array.</p><p>so:<br
/> $max = count( $array);<br
/> for( $i=0; $&lt;$max; $i++){<br
/> $array[$i] = &#039;Do whatever you want to do&#039;;<br
/> }</p><p>is way faster than foreach.</p><p>cheers, Frank</p> ]]></content:encoded> </item> <item><title>By: mike stevens</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-10705</link> <dc:creator>mike stevens</dc:creator> <pubDate>Sun, 24 Jan 2010 19:58:17 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-10705</guid> <description>Very nice article.  The cross language examples are insightful</description> <content:encoded><![CDATA[<p>Very nice article.  The cross language examples are insightful</p> ]]></content:encoded> </item> <item><title>By: tog000</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-10701</link> <dc:creator>tog000</dc:creator> <pubDate>Sat, 23 Jan 2010 16:09:58 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-10701</guid> <description>In my opinion, Mootools has the most powerful &quot;foreach&quot; emulation for Javascript:myArray = new Array(&quot;item&quot;, &quot;item&quot;, &quot;item&quot;);
myArray.each(function(element,index){
myArray[index] = element+index;
})This will produce: {&quot;item0&quot;,&quot;item1&quot;,&quot;item2&quot;}</description> <content:encoded><![CDATA[<p>In my opinion, Mootools has the most powerful &#8220;foreach&#8221; emulation for Javascript:</p><p>myArray = new Array(&#8220;item&#8221;, &#8220;item&#8221;, &#8220;item&#8221;);<br
/> myArray.each(function(element,index){<br
/> myArray[index] = element+index;<br
/> })</p><p>This will produce: {&#8220;item0&#8243;,&#8221;item1&#8243;,&#8221;item2&#8243;}</p> ]]></content:encoded> </item> <item><title>By: Chris Coyier</title><link>http://www.cssnewbie.com/emulate-a-foreach-loop-in-javascript/comment-page-1/#comment-10664</link> <dc:creator>Chris Coyier</dc:creator> <pubDate>Fri, 22 Jan 2010 19:04:15 +0000</pubDate> <guid
isPermaLink="false">http://www.cssnewbie.com/?p=511#comment-10664</guid> <description>I was always told that you should set a variable to the myArray.length first, that way it doesn&#039;t have to calculate that number on every iteration. And for some reason, ++i is faster that i++ I&#039;ve been told, but I have no idea why.</description> <content:encoded><![CDATA[<p>I was always told that you should set a variable to the myArray.length first, that way it doesn&#8217;t have to calculate that number on every iteration. And for some reason, ++i is faster that i++ I&#8217;ve been told, but I have no idea why.</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)

Served from: www.cssnewbie.com @ 2010-09-07 23:50:03 -->