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
2008-09-24 23:18:22
+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
2008-09-24 23:20:14
+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
2008-09-24 23:23:53
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
2010-03-05 12:54:20
Important thing to remember is not to use keywords as field names without quotes. E.g:{foo:"bar",default:baz} // oopsie!
yk4ever
2010-06-01 11:35:23
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
2010-07-08 22:09:55
A:
If you require your keys to be be any object rather than just strings then you could use my jshashtable.
Tim Down
2010-06-01 11:32:42