<?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: Looping through object properties and hash tables using the &#8220;in&#8221; statement</title>
	<atom:link href="http://www.javascriptkata.com/2007/04/01/looping-through-object-properties-and-hash-tables-using-the-in-statement/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptkata.com/2007/04/01/looping-through-object-properties-and-hash-tables-using-the-in-statement/</link>
	<description>Advanced katas for javascripters</description>
	<lastBuildDate>Fri, 30 Jul 2010 18:10:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Dave</title>
		<link>http://www.javascriptkata.com/2007/04/01/looping-through-object-properties-and-hash-tables-using-the-in-statement/comment-page-1/#comment-456</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Wed, 05 Mar 2008 17:07:22 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=19#comment-456</guid>
		<description>One year later... Was just googling for some syntax clarification and I saw this post.

&quot;in&quot; can also be used in an expression, i.e. 

if (&#039;index&#039; in { index: 0 }) // do stuff
else // do other stuff</description>
		<content:encoded><![CDATA[<p>One year later&#8230; Was just googling for some syntax clarification and I saw this post.</p>
<p>&#8220;in&#8221; can also be used in an expression, i.e. </p>
<p>if (&#8216;index&#8217; in { index: 0 }) // do stuff<br />
else // do other stuff</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.javascriptkata.com/2007/04/01/looping-through-object-properties-and-hash-tables-using-the-in-statement/comment-page-1/#comment-57</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Tue, 03 Apr 2007 14:39:47 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=19#comment-57</guid>
		<description>@BK : I thought about creating and intermediate belt but it&#039;s just too hard for the user to understand. White and black belts are known by everyone.

@Aaron : Thanks for the correction. I was too lazy to write code without an additional &quot;,&quot; at the end.

Thanks also to be such a great commenter. Even if your comments are most of the time caught by Akismet as spam ;)</description>
		<content:encoded><![CDATA[<p>@BK : I thought about creating and intermediate belt but it&#8217;s just too hard for the user to understand. White and black belts are known by everyone.</p>
<p>@Aaron : Thanks for the correction. I was too lazy to write code without an additional &#8220;,&#8221; at the end.</p>
<p>Thanks also to be such a great commenter. Even if your comments are most of the time caught by Akismet as spam <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Faanes</title>
		<link>http://www.javascriptkata.com/2007/04/01/looping-through-object-properties-and-hash-tables-using-the-in-statement/comment-page-1/#comment-59</link>
		<dc:creator>Aaron Faanes</dc:creator>
		<pubDate>Mon, 02 Apr 2007 15:29:55 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=19#comment-59</guid>
		<description>Looping through objects like that is easier, but that means you&#039;ll end up with the string of keys with a comma at the end (since you do += name + &quot;, &quot;;).

var properties = [];
for(var name in obj) {
properties.push(name);
}
alert(properties.join(&quot;, &quot;);

Will probably get you what you want. If you have Firebug, you could also use console.log to get a message in the log, instead of a popup dialog. (I really can&#039;t emphasize enough the amazingness of _not_ having to alert() your log messages)

Of course, you could also combine the two for-loops into:

var properties = [];
var functions = [];

for(var name in obj) {
if(obj[name] instanceof Function)
functions.push(name);
else
properties.push(name);
}

You also probably&#039;d want to join() the stuff in alertClass, otherwise you&#039;ll end up with &quot;The class The full name of the class51&quot;. (By alert([this.name, this.fullName, this.age].join(&quot; &quot;)); )

One important thing that I mentioned in another post is this very common bug with for(var i in obj).

Let&#039;s say you want to sum some numbers. So you want to take element, and then add it with the next, and so forth.

var numbers = [1,2,3,4];
var sums = [];
for(var i in numbers) {
if((i + 1) in numbers) // So that we don&#039;t add a non-existent fifth element.
sums.push(numbers[i] + numbers[i + 1]);
}

You&#039;d probably assume (once you work through the &quot;cleverness&quot;) that you&#039;d have [3, 5, 7] in sums, but sums will actually by empty. The reason for this is in the numbers[i + 1]. Javascript tries to be smart and do type coercion for you (0 == &quot;0&quot;) which is convenient alot of the time. However, here, i is &quot;0&quot;, &quot;1&quot;, etc., and _not_ 0, 1, 2. Javascript assumes you want strings over numbers, so it coerces the 1 to a string, instead of the i to a number, giving you &quot;01&quot;, &quot;11&quot;, &quot;21&quot; as key values, which fails every time in our non-existent-fifth-element test.

It&#039;s a contrived example here, but it will happen to everyone out there, and it&#039;s very insidious since it looks so innocent and correct.

 - Aaron</description>
		<content:encoded><![CDATA[<p>Looping through objects like that is easier, but that means you&#8217;ll end up with the string of keys with a comma at the end (since you do += name + &#8220;, &#8220;;).</p>
<p>var properties = [];<br />
for(var name in obj) {<br />
properties.push(name);<br />
}<br />
alert(properties.join(&#8220;, &#8220;);</p>
<p>Will probably get you what you want. If you have Firebug, you could also use console.log to get a message in the log, instead of a popup dialog. (I really can&#8217;t emphasize enough the amazingness of _not_ having to alert() your log messages)</p>
<p>Of course, you could also combine the two for-loops into:</p>
<p>var properties = [];<br />
var functions = [];</p>
<p>for(var name in obj) {<br />
if(obj[name] instanceof Function)<br />
functions.push(name);<br />
else<br />
properties.push(name);<br />
}</p>
<p>You also probably&#8217;d want to join() the stuff in alertClass, otherwise you&#8217;ll end up with &#8220;The class The full name of the class51&#8243;. (By alert([this.name, this.fullName, this.age].join(&#8221; &#8220;)); )</p>
<p>One important thing that I mentioned in another post is this very common bug with for(var i in obj).</p>
<p>Let&#8217;s say you want to sum some numbers. So you want to take element, and then add it with the next, and so forth.</p>
<p>var numbers = [1,2,3,4];<br />
var sums = [];<br />
for(var i in numbers) {<br />
if((i + 1) in numbers) // So that we don&#8217;t add a non-existent fifth element.<br />
sums.push(numbers[i] + numbers[i + 1]);<br />
}</p>
<p>You&#8217;d probably assume (once you work through the &#8220;cleverness&#8221;) that you&#8217;d have [3, 5, 7] in sums, but sums will actually by empty. The reason for this is in the numbers[i + 1]. Javascript tries to be smart and do type coercion for you (0 == &#8220;0&#8243;) which is convenient alot of the time. However, here, i is &#8220;0&#8243;, &#8220;1&#8243;, etc., and _not_ 0, 1, 2. Javascript assumes you want strings over numbers, so it coerces the 1 to a string, instead of the i to a number, giving you &#8220;01&#8243;, &#8220;11&#8243;, &#8220;21&#8243; as key values, which fails every time in our non-existent-fifth-element test.</p>
<p>It&#8217;s a contrived example here, but it will happen to everyone out there, and it&#8217;s very insidious since it looks so innocent and correct.</p>
<p> &#8211; Aaron</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: BK</title>
		<link>http://www.javascriptkata.com/2007/04/01/looping-through-object-properties-and-hash-tables-using-the-in-statement/comment-page-1/#comment-58</link>
		<dc:creator>BK</dc:creator>
		<pubDate>Mon, 02 Apr 2007 15:09:53 +0000</pubDate>
		<guid isPermaLink="false">http://javascriptkata.timmyontime.com/?p=19#comment-58</guid>
		<description>Are you sure this should not be listed under white belt? I think this is still simple language semantics. Or maybe blue belt! :P</description>
		<content:encoded><![CDATA[<p>Are you sure this should not be listed under white belt? I think this is still simple language semantics. Or maybe blue belt! <img src='http://www.javascriptkata.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
