views:

12774

answers:

3

Possible Duplicate:
Can anyone recommend a good Hashtable implementation in Javascript?

Hello everyone,

I want to calculate/store some statistics information using JavaScript, the equivalent code in C# is below (features I need are -- key-value pair, string/int key value pair, manipulate values by keys, etc.), any ideas how to implement the same function in JavaScript? Looks like there is no built-in Dictionary or Hashtable? Thanks!

Dictionary<string, int> statistics;

statistics["Foo"] = 10;
statistics["Goo"] = statistics["Goo"] + 1;
statistics.Add("Zoo", 1);

regards, George

+1  A: 

There is nothing built in, though, both are simply wrappers for arrays which are supported by javascript and common array functions which you can write yourself. You can either search google for an implementation or make your own.

Spencer Ruport
Good to know no built-in hashtable.
George2
+7  A: 

Depending on what you want to do, you may want to try using JavaScript objects as associative arrays: see JavaScript Associative Arrays Demystified.

Alek Davis
Thanks Alek, working
George2
+5  A: 
var associativeArray = [];
associativeArray["one"] = "First";
associativeArray["two"] = "Second";
associativeArray["three"] = "Third";

If you are comming from an Object Oriented language you should check this article

Dani Cricco
Very nice sample, thanks!
George2