<?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>The Developer Day &#187; enumerate</title>
	<atom:link href="http://www.thedeveloperday.com/tag/enumerate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thedeveloperday.com</link>
	<description>Staying Curious</description>
	<lastBuildDate>Fri, 03 Feb 2012 12:03:22 +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>JavaScript Associative Arrays &amp; Iterations</title>
		<link>http://www.thedeveloperday.com/iterate-javascript-associative-arrays/</link>
		<comments>http://www.thedeveloperday.com/iterate-javascript-associative-arrays/#comments</comments>
		<pubDate>Mon, 06 Aug 2007 14:48:42 +0000</pubDate>
		<dc:creator>Žilvinas Šaltys</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[associative]]></category>
		<category><![CDATA[enumerate]]></category>
		<category><![CDATA[iterators]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.thedeveloperday.com/iterate-javascript-associative-arrays/</guid>
		<description><![CDATA[Today I was working with a little piece of JavaScript code and writing something like this: var tmp = new Array(); tmp['id']= '123'; tmp['name'] = 'bla bla'; And then i tried to iterate over the array like this: for (var i in tmp) { alert(i); }; If you haven&#8217;t done a lot of JavaScript you [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was working with a little piece of JavaScript code and writing something like this:</p>
<pre name="code" class="js:nogutter">var tmp = new Array();
tmp['id']= '123';
tmp['name'] = 'bla bla';</pre>
<p>And then i tried to iterate over the array like this:</p>
<pre name="code" class="js:nogutter">for (var i in tmp) { alert(i); };</pre>
<p>If you haven&#8217;t done a lot of JavaScript you would probably say that the above iteration should output &#8216;id&#8217;, &#8216;name&#8217;. Surprisingly you may be wrong. If you are using any code that extends the Array prototype with attributes or functions you will see them in the output. This may happen if you are using the <a href="http://www.prototypejs.org/" title="popular javascript framework">prototype framework</a> or <a href="http://www.json.org/json.js" title="default json stringifier">The open source code of a JSON parser and JSON stringifier</a>. All you&#8217;re doing is setting properties on an object. After reading a few blog posts and comments on this I found yet another issue that ideally I shouldn&#8217;t be using new Array() and using new Object(). That sounds kind of true because JavaScript does not support associative arrays and you could do the same on any object. Date, RegExp, Boolean or any other. Actually using Object solved the prototype framework problem, because prototype framework does not extend the Object prototype since 1.4 but the <a href="http://www.json.org/json.js" title="default javascript json library">JSON stringifier</a> does.</p>
<p>After googling a while I found out a little tricksy that may mostly work with this problem.  You could do something like this:</p>
<pre name="code" class="js:nogutter">for (var i in obj) {
if (!obj.propertyIsEnumerable(i)) continue;
foo(obj[i]);
}</pre>
<p>This only works if properties are enumerable. That may not always save you especially if you are using any 3rd party software that breaks this, but it certainly helps you while solving the prototype framework or JSON stringifier puzzle.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thedeveloperday.com/iterate-javascript-associative-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

