Monday, February 20, 2006

Hashtables in JavaScript

[This was originally posted at]

As a bit of trivia for the day, JavaScript can handle pseudo-hashtables, via the Array object. You can reference the item with its key, and enumerate through all the objects.

function DoHashTest()
  var h = new Array();
  h["aaa"] = "Aardvark";
  h["bbb"] = "Babboon";
  var s = "";
  //This won't work, length will be 0.
  for (var i = 0; i < h.length; i++)
     s += h[i];

  //Cycle through like a hash:
  for (var i in h)
    s += i;

No comments:

Post a Comment