<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Real programming with JavaScript - Associative Arrays</title>
	<link>http://blog.thingoid.com/2004/08/real-javascript-hash/</link>
	<description></description>
	<pubDate>Thu, 28 Aug 2008 03:46:48 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Simon Norris</title>
		<link>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-5935</link>
		<dc:creator>Simon Norris</dc:creator>
		<pubDate>Wed, 20 Feb 2008 10:26:34 +0000</pubDate>
		<guid>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-5935</guid>
		<description>&lt;p&gt;This may just be a lazy hack but it works for me. I've been unable to sort an associative array or find a script which works so...&lt;/p&gt;

&lt;p&gt;Lazy, lazy, lazy: concatenate it in an ordinary array, sort that and split the values into a new associative array. Probably not recommended for the phone book but like a say - works for me.&lt;/p&gt;

&lt;p&gt;Also removes duplicates&lt;/p&gt;

&lt;p&gt;function sortAssoc(inputArray) {
    var tempArray = new Array();
    var outputArray = new Array();&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;i=0;

for (keyVar in inputArray) {
    tempArray[i] = inputArray[keyVar] + '//--string_divider--//' + keyVar;
    i++;
}

tempArray = tempArray.sort();

for (i=0; i &#60; tempArray.length; i++) {
    if (tempArray[i]!=tempArray[i-1]) {
        temp = tempArray[i].split('//--string_divider--//');
        tempIndex = temp[1];
        outputArray[tempIndex] = temp[0];
    }
}

return outputArray;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This may just be a lazy hack but it works for me. I&#8217;ve been unable to sort an associative array or find a script which works so&#8230;</p>
<p>Lazy, lazy, lazy: concatenate it in an ordinary array, sort that and split the values into a new associative array. Probably not recommended for the phone book but like a say - works for me.</p>
<p>Also removes duplicates</p>
<p>function sortAssoc(inputArray) {<br />
    var tempArray = new Array();<br />
    var outputArray = new Array();</p>
<pre><code>i=0;

for (keyVar in inputArray) {
    tempArray[i] = inputArray[keyVar] + '//--string_divider--//' + keyVar;
    i++;
}

tempArray = tempArray.sort();

for (i=0; i &lt; tempArray.length; i++) {
    if (tempArray[i]!=tempArray[i-1]) {
        temp = tempArray[i].split('//--string_divider--//');
        tempIndex = temp[1];
        outputArray[tempIndex] = temp[0];
    }
}

return outputArray;
</code></pre>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Trevor</title>
		<link>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-89</link>
		<dc:creator>Trevor</dc:creator>
		<pubDate>Tue, 04 Apr 2006 21:03:20 +0000</pubDate>
		<guid>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-89</guid>
		<description>&lt;p&gt;Hi Ed,&lt;/p&gt;

&lt;p&gt;An intriguing problem, although I'm not sure I understand what you're trying to do so I hope my answer makes some sense.&lt;/p&gt;

&lt;p&gt;I don't think JavaScript has this functionality built-in. It may be possible to build a user-defined function that would do this but the simplest solution I can think of is to keep an ordinary array of the hash table keys. Something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var keyIndex = new Array();
var hashTable = new Object();&lt;/p&gt;

&lt;p&gt;hashTable["z"] = "the last item";
keyIndex.push("z");&lt;/p&gt;

&lt;p&gt;hashTable["x"] = "the first item";
keyIndex.push("x");&lt;/p&gt;

&lt;p&gt;hashTable["y"] = "the middle item";
keyIndex.push("y");
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can then use the Array() object's built-in sort() function to order the keys and loop through the keyIndex array to give you your keys in order:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;keyIndex.sort();&lt;/p&gt;

&lt;p&gt;var i;
for (i in keyIndex)
{
    document.write(hashTable[ keyIndex[i] ] + "&#60;br /&#62;");
}
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This should produce the output:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;the first item
the middle item
the last item
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There's a bit of overhead in managing both the keyIndex and hashTable objects so I don't think it will be particularly fast. And I don't think it's a very elegant solution but at least it's &lt;em&gt;an option&lt;/em&gt; for you. I'm sorry if it's not really what you're looking for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please note:&lt;/strong&gt; I haven't tested this example code, so no guarantees that it'll work!&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

&lt;p&gt;Trevor.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hi Ed,</p>
<p>An intriguing problem, although I&#8217;m not sure I understand what you&#8217;re trying to do so I hope my answer makes some sense.</p>
<p>I don&#8217;t think JavaScript has this functionality built-in. It may be possible to build a user-defined function that would do this but the simplest solution I can think of is to keep an ordinary array of the hash table keys. Something like this:</p>
<p><code>var keyIndex = new Array();<br />
var hashTable = new Object();</code></p>
<p>hashTable[&#8221;z&#8221;] = &#8220;the last item&#8221;;<br />
keyIndex.push(&#8221;z&#8221;);</p>
<p>hashTable[&#8221;x&#8221;] = &#8220;the first item&#8221;;<br />
keyIndex.push(&#8221;x&#8221;);</p>
<p>hashTable[&#8221;y&#8221;] = &#8220;the middle item&#8221;;<br />
keyIndex.push(&#8221;y&#8221;);
</p>
<p>You can then use the Array() object&#8217;s built-in sort() function to order the keys and loop through the keyIndex array to give you your keys in order:</p>
<p><code>keyIndex.sort();</code></p>
<p>var i;<br />
for (i in keyIndex)<br />
{<br />
    document.write(hashTable[ keyIndex[i] ] + &#8220;&lt;br /&gt;&#8221;);<br />
}
</p>
<p>This should produce the output:</p>
<pre><code>the first item
the middle item
the last item
</code></pre>
<p>There&#8217;s a bit of overhead in managing both the keyIndex and hashTable objects so I don&#8217;t think it will be particularly fast. And I don&#8217;t think it&#8217;s a very elegant solution but at least it&#8217;s <em>an option</em> for you. I&#8217;m sorry if it&#8217;s not really what you&#8217;re looking for.</p>
<p><strong>Please note:</strong> I haven&#8217;t tested this example code, so no guarantees that it&#8217;ll work!</p>
<p>Cheers,</p>
<p>Trevor.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ed</title>
		<link>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-85</link>
		<dc:creator>Ed</dc:creator>
		<pubDate>Mon, 03 Apr 2006 00:12:22 +0000</pubDate>
		<guid>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-85</guid>
		<description>&lt;p&gt;Hey Trevor, thanks for the helpful articles.  You said, "The elements in associative arrays don't have a logical sequence, so I guess counting them doesn't necessarily make sense."&lt;/p&gt;

&lt;p&gt;What if you WANT your elements to have a logical sequence (say, alphabetical).  For example,&lt;/p&gt;

&lt;p&gt;var map = new Object();&lt;/p&gt;

&lt;p&gt;var foo = [x, y, z];
var bar = [v, w, z];
var baz = [a, b, c];&lt;/p&gt;

&lt;p&gt;I want to be able to add foo to map, and then bar, and have bar be "before" foo (because it comes first alphabetically).  If I later add baz, I want it to be "between" bar and foo for the same reason.&lt;/p&gt;

&lt;p&gt;I get why associative arrays don't have any logical sequence, and so adding 'in sequence' doesn't really make sense.  What I'm describing (and wanting!) is more like the Java TreeMap-like objects, which maintains the keys in order and guarantees quick access (I need both).&lt;/p&gt;

&lt;p&gt;What would solve this for me is a way to sort() the keys of an associative array/object (maintaining the associated element relationships of course).&lt;/p&gt;

&lt;p&gt;I've searched for both these solutions but haven't found one yet.  I thought I'd give you a try and see if you had any ideas.&lt;/p&gt;

&lt;p&gt;Thanks in advance!&lt;/p&gt;

&lt;p&gt;e.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hey Trevor, thanks for the helpful articles.  You said, &#8220;The elements in associative arrays don&#8217;t have a logical sequence, so I guess counting them doesn&#8217;t necessarily make sense.&#8221;</p>
<p>What if you WANT your elements to have a logical sequence (say, alphabetical).  For example,</p>
<p>var map = new Object();</p>
<p>var foo = [x, y, z];<br />
var bar = [v, w, z];<br />
var baz = [a, b, c];</p>
<p>I want to be able to add foo to map, and then bar, and have bar be &#8220;before&#8221; foo (because it comes first alphabetically).  If I later add baz, I want it to be &#8220;between&#8221; bar and foo for the same reason.</p>
<p>I get why associative arrays don&#8217;t have any logical sequence, and so adding &#8216;in sequence&#8217; doesn&#8217;t really make sense.  What I&#8217;m describing (and wanting!) is more like the Java TreeMap-like objects, which maintains the keys in order and guarantees quick access (I need both).</p>
<p>What would solve this for me is a way to sort() the keys of an associative array/object (maintaining the associated element relationships of course).</p>
<p>I&#8217;ve searched for both these solutions but haven&#8217;t found one yet.  I thought I&#8217;d give you a try and see if you had any ideas.</p>
<p>Thanks in advance!</p>
<p>e.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Trevor</title>
		<link>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-52</link>
		<dc:creator>Trevor</dc:creator>
		<pubDate>Fri, 03 Feb 2006 02:02:19 +0000</pubDate>
		<guid>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-52</guid>
		<description>&lt;h3 id="multidimensional"&gt;Faking a multidimensional hash&lt;/h3&gt;

&lt;p&gt;Hi Eugene,&lt;/p&gt;

&lt;p&gt;As you suggest, a multidimensional hash can be made by making a series of objects inside the first hash.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;varName[1] = new Object();
varName[2] = new Object();
... and so on&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's a perfectly acceptable way to do it.&lt;/p&gt;

&lt;p&gt;However, if you find that's a bit of overkill you can also fake it just by concatenating the key values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var varName = new Object()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var i = 1 + "z" + 33;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;varName[i] = 1;
varName[1 + "y" + 34] = 2;
varName[2 + "aa" + 'a'] = 3;
varName[2 + "aa" + 'b'] = 4;
varName[2 + "aa" + 'c'] = 5;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;i = "2aaa";
alert(varName["1z33"]);
alert(varName[i]);
alert(varName[2 + "aa" + 'c']);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you prefer your multiple dimensions separated, say by a comma, just include it in the concatenation:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;varName[1 + "," + "z" + "," + 33] = 1;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;alert(varName["1,z,33"]);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I'd recommend this both for readability and especially if you're using numeric keys (remember that 1 + 2 + 3 is not the concatenated value "123", it's the numeric value 6).&lt;/p&gt;

&lt;p&gt;Sure it's a hack, but it's legit: it doesn't matter how you make your hash indexes as long as each one is unique.&lt;/p&gt;

&lt;p&gt;Cheers,&lt;/p&gt;

&lt;p&gt;Trevor.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<h3 id="multidimensional">Faking a multidimensional hash</h3>
<p>Hi Eugene,</p>
<p>As you suggest, a multidimensional hash can be made by making a series of objects inside the first hash.</p>
<p><code>varName[1] = new Object();<br />
varName[2] = new Object();<br />
... and so on</code></p>
<p>That&#8217;s a perfectly acceptable way to do it.</p>
<p>However, if you find that&#8217;s a bit of overkill you can also fake it just by concatenating the key values:</p>
<p><code>var varName = new Object()</code></p>
<p><code>var i = 1 + "z" + 33;</code></p>
<p><code>varName[i] = 1;<br />
varName[1 + "y" + 34] = 2;<br />
varName[2 + "aa" + 'a'] = 3;<br />
varName[2 + "aa" + 'b'] = 4;<br />
varName[2 + "aa" + 'c'] = 5;</code></p>
<p><code>i = "2aaa";<br />
alert(varName["1z33"]);<br />
alert(varName[i]);<br />
alert(varName[2 + "aa" + 'c']);</code></p>
<p>If you prefer your multiple dimensions separated, say by a comma, just include it in the concatenation:</p>
<p><code>varName[1 + "," + "z" + "," + 33] = 1;</code></p>
<p><code>alert(varName["1,z,33"]);</code></p>
<p>I&#8217;d recommend this both for readability and especially if you&#8217;re using numeric keys (remember that 1 + 2 + 3 is not the concatenated value &#8220;123&#8243;, it&#8217;s the numeric value 6).</p>
<p>Sure it&#8217;s a hack, but it&#8217;s legit: it doesn&#8217;t matter how you make your hash indexes as long as each one is unique.</p>
<p>Cheers,</p>
<p>Trevor.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eugene</title>
		<link>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-50</link>
		<dc:creator>Eugene</dc:creator>
		<pubDate>Thu, 02 Feb 2006 13:17:06 +0000</pubDate>
		<guid>http://blog.thingoid.com/2004/08/real-javascript-hash/#comment-50</guid>
		<description>&lt;p&gt;well ... how can i create multidimensional hash ?
eg:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;varName[1]["z"][33] = 1;
varName[1]["y"][34] = 2;
varName[2]["aa"]['a'] = 3;
varName[2]["aa"]['b'] = 4;
varName[2]["aa"]['c'] = 5;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;without varName[1] = new Object();&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>well &#8230; how can i create multidimensional hash ?<br />
eg:</p>
<p><code>varName[1]["z"][33] = 1;<br />
varName[1]["y"][34] = 2;<br />
varName[2]["aa"]['a'] = 3;<br />
varName[2]["aa"]['b'] = 4;<br />
varName[2]["aa"]['c'] = 5;</code></p>
<p>without varName[1] = new Object();</p>
]]></content:encoded>
	</item>
</channel>
</rss>
