<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Alice, Bob, and Mallory: JavaScript hash table keys</title>
    <link>http://www.alicebobandmallory.com/articles/2010/03/05/javascript-hash-table-keys</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>metasyntactics</description>
    <item>
      <title>JavaScript hash table keys</title>
      <description>&lt;p&gt;In JavaScript you can add properties to objects dynamically. You can access those properties both by &lt;code&gt;object.foo&lt;/code&gt; and &lt;code&gt;object['foo']&lt;/code&gt;. The later is commonly used to use JavaScript objects as &lt;a href="http://en.wikipedia.org/wiki/Hash_table"&gt;hash tables&lt;/a&gt; (associative arrays).&lt;/p&gt;

&lt;p&gt;While implementing &lt;a href="http://stackoverflow.com/questions/2380019/generate-8-unique-random-numbers-between-1-and-100/2380513#2380513"&gt;a simplistic unique random number generator&lt;/a&gt; I happened to use &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Object/keys"&gt;keys(obj)&lt;/a&gt;. Unfortunately &lt;code&gt;keys(obj)&lt;/code&gt; is part of &lt;a href="http://en.wikipedia.org/wiki/ECMAScript"&gt;ECMAScript&lt;/a&gt; 5. See chapter 15.2.3.14 in &lt;a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf"&gt;ECMA-262&lt;/a&gt;. The web browsers of today mostly implements ECMAScript 3.&lt;/p&gt;

&lt;p&gt;Here's an implementation of &lt;code&gt;keys(obj)&lt;/code&gt; for ECMAScript 3 browsers (tested in Google Chrome, IE8 and Firefox 3.5). If the browser already has a keys function then nothing will be done.&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;7&lt;tt&gt;
&lt;/tt&gt;8&lt;tt&gt;
&lt;/tt&gt;9&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (typeof keys == &lt;span style="background-color:#fff0f0;color:#D20"&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;span style=""&gt;undefined&lt;/span&gt;&lt;span style="color:#710"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) &lt;tt&gt;
&lt;/tt&gt;{ &lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; keys = &lt;span style="color:#080;font-weight:bold"&gt;function&lt;/span&gt;(ob) &lt;tt&gt;
&lt;/tt&gt;  {&lt;tt&gt;
&lt;/tt&gt;    props=[];&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (k in ob) &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (ob.hasOwnProperty(k)) props.push(k);&lt;tt&gt;
&lt;/tt&gt;    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; props;&lt;tt&gt;
&lt;/tt&gt;  }&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
The simplistic unique random number generator looks like this&lt;/p&gt;

&lt;table class="CodeRay"&gt;&lt;tr&gt;
  &lt;td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"&gt;&lt;pre&gt;1&lt;tt&gt;
&lt;/tt&gt;2&lt;tt&gt;
&lt;/tt&gt;3&lt;tt&gt;
&lt;/tt&gt;4&lt;tt&gt;
&lt;/tt&gt;5&lt;tt&gt;
&lt;/tt&gt;6&lt;tt&gt;
&lt;/tt&gt;&lt;/pre&gt;&lt;/td&gt;
  &lt;td class="code"&gt;&lt;pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"&gt;&lt;span style="color:#080;font-weight:bold"&gt;function&lt;/span&gt; uniqueRndNumbers(min, max, quantity) {&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#339;font-weight:bold"&gt;var&lt;/span&gt; ht={}, i=quantity;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;while&lt;/span&gt; ( i&amp;gt;&lt;span style="color:#00D;font-weight:bold"&gt;0&lt;/span&gt; || keys(ht).length&amp;lt;quantity) &lt;tt&gt;
&lt;/tt&gt;    ht[Math.floor(Math.random()*(max-min+&lt;span style="color:#00D;font-weight:bold"&gt;1&lt;/span&gt;))+min]=i--;&lt;tt&gt;
&lt;/tt&gt;  &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; keys(ht);&lt;tt&gt;
&lt;/tt&gt;}&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;

&lt;p&gt;&lt;br/&gt;
This function has not undergone any serious testing. Also if the &lt;code&gt;quantity&lt;/code&gt; is more than a fraction of &lt;code&gt;(max-min)&lt;/code&gt; then another algorithm like the &lt;a href="http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle"&gt;Fisher–Yates shuffle&lt;/a&gt; might be a better choice.&lt;/p&gt;</description>
      <pubDate>Fri, 05 Mar 2010 17:42:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:27e04911-d089-4122-8a96-ad529a9b8b3b</guid>
      <author>Jonas Elfström</author>
      <link>http://www.alicebobandmallory.com/articles/2010/03/05/javascript-hash-table-keys</link>
      <category>JavaScript</category>
    </item>
    <item>
      <title>"JavaScript hash table keys" by Jonas Elfström</title>
      <description>&lt;p&gt;Nice catch, fixed. Thanks!&lt;/p&gt;</description>
      <pubDate>Fri, 15 Oct 2010 12:47:07 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:ed3d3238-8f43-4c6d-b36b-ae38f5c44c2b</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/03/05/javascript-hash-table-keys#comment-4391</link>
    </item>
    <item>
      <title>"JavaScript hash table keys" by Ned</title>
      <description>&lt;p&gt;It looks like you&amp;#8217;ve got a copy/paste error in your first snippet. &amp;#8220;for (k in ht)&amp;#8221; should probably be &amp;#8220;for (k in obj)&amp;#8221; now that the loop&amp;#8217;s been moved to its own function.&lt;/p&gt;</description>
      <pubDate>Thu, 14 Oct 2010 22:54:38 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:11866833-cfbd-4d18-a744-e0a5c7a1f65f</guid>
      <link>http://www.alicebobandmallory.com/articles/2010/03/05/javascript-hash-table-keys#comment-4382</link>
    </item>
  </channel>
</rss>

