Link to home
Start Free TrialLog in
Avatar of yongsing
yongsing

asked on

Hashtable

Is there a hashtable object in javascript that I can use?

For example, I have the following values:

1W
1M
2M
...

And I want to map to these values:

1 Week
1 Month
2 Months
...
ASKER CERTIFIED SOLUTION
Avatar of gksinghiet
gksinghiet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dakyd
dakyd

Actually, you can use an array just like a hash.  It's often called an associative array, but, for all intents and purposes, it is a hash.  For example:

<script type="text/javascript">
var hash = new Array();
hash["a"] = "foo";
hash["b"] = "bar";
hash["c"] = "baz";

// first, show you can get each element back out
var str = "";
str += "in my hash, key 'a' has value of: " + hash["a"];
str += "\nin my hash, key 'b' has value of: " + hash["b"];
str += "\nin my hash, key 'c' has value of: " + hash["c"];
alert(str);

// now, getting a collection of keys (and values)
var keyStr = "";
var valStr = "";
for (var i in hash)
{
  keyStr += i +"\n";
  valStr += hash[i] + "\n";
}
alert("keys:\n" + keyStr + "values:\n" + valStr);
</script>

Hope that helps.