Standard JavaScript is included in the support hash associative array, after finding data and testing, about standard JavaScript contains hash associative array operation memo as follows
1. Hash associative array definition
Define an empty array Myhash = {}
Directly define the array Myhash = {"Key1″:" Val1″, "Key2″:" Val2″}
Define Array Myhash = new array with array (); myhash["Key1″] =" Val1″; myhash["Key2″] =" Val2″;
2. Add a key value to a hash associative array
Add a new key Newkey, the key value is newval myhash["newkey"] = "newval";
3. Delete Hash associative array already has key value
Delete a key newkey, at the same time, the key value corresponding to the newval also disappeared delete myhash["Newkey"];
4. Traversing hash associative arrays
Traverse the entire hash array for (key in Myhash) {val = Myhash[key];}
5. Example of simple use of hash associative array
Steering script <script type= "Text/javascript" > urlhash = {"Yahoo": "www.yahoo.cn", "Baidu": "Www.baidu.com", "Google": " Www.google.cn "};
Interactive use Example userinfo = prompt ("Please enter your most wanted search engine: (yahoo|baidu|google)", "Yahoo"); document.write ("Your choice:" + UserInfo + ", <a href=http://" + GetURL (userinfo) + "target=_blank>" + "Click here to enter" + "</a& gt; "+ UserInfo +". ”);
GetURL//If the parameter is undefined, the default return www.yahoo.cn URL//@param choice select name//@return URL actual URL function getURL (choice) {URL = urlh Ash[choice]; if (typeof (Urlhash[choice]) = = "undefined") url = "www.yahoo.cn"; return URL; }
Get hash list of all keys//@param hash hash array//@return keys Key name data function Array_keys (hash) {keys = []; for (key in hash) Ke Ys.push (key); return keys; } </script>
JS Associative array