views:

1636

answers:

5

I've found jCache and some other home-grown methods using associative arrays. If you've had experience with jCache, were there any limitations?

+7  A: 

Since every object in JS behaves like - and is generally implemented as - a hashtable, i just go with that...

var hashSweetHashTable = {};
Shog9
+1  A: 

Could you provide more info about your specific requirements for a JavaScript Hashtable? In the most generic case you can use an arbitrary JavaScript object:

var obj = { Name : "John", Age: 15};
alert(obj.Name); //displays "John"
korchev
+15  A: 

Unless you have a specific reason not to, just use a normal object. Object properties in Javascript can be referenced using hashtable-style syntax:

var hashtable = {};
hashtable.foo = "bar";
hashtable['bar'] = "foo";

Can then be referenced as:

hashtable['foo'];
// or
hashtable.bar;

Of course this does mean your keys have to be strings.

roryf
Keys as integers caused me no problem. http://stackoverflow.com/questions/2380019/generate-8-unique-random-numbers-between-1-and-100/2380513#2380513
Jonas Elfström
Important thing to remember is not to use keywords as field names without quotes. E.g:{foo:"bar",default:baz} // oopsie!
yk4ever
Jonas: bear in mind that your integers are converted to strings when the property is being set: `var hash = {}; hash[1] = "foo"; alert(hash["1"]);` alerts "foo".
Tim Down
A: 

http://www.skidvn.com/jcache

Ritesh M Nayak
A: 

If you require your keys to be be any object rather than just strings then you could use my jshashtable.

Tim Down