Address: http://wyllife.blog.163.com/blog/static/4116390120115210038676/
1. Associate an array (hash array)
Standard JavaScript contains arrays that support hash Association. After searching for information and testing, the operations on the hash Association array contained in standard JavaScript are as follows:
1. Hash join array Definition
// Define an empty array
Myhash = {}
// Directly define an array
Myhash = {"key1": "val1", "key2": "val2 ″}
// Define an array with Array
Myhash = new array ();
Myhash ["key1"] = "val1 ″;
Myhash ["key2"] = "val2 ″;
2. Add a key value to the hash join Array
// Add a new key newkey with the key value newval
Myhash ["newkey"] = "newval ";
3. Deletes an existing key value from the hash associated array.
// Delete a key newkey. At the same time, the newval corresponding to the key value disappears.
Delete myhash ["newkey"];
4. Traverse the hash associated array
// Traverse the entire hash Array
For (key in myhash ){
Val = myhash [Key];
}
5. Simple use example of hash join Array
// Redirection script
<SCRIPT type = "text/JavaScript">
Urlhash = {"Yahoo": "www.yahoo.cn",
"Baidu": "www.baidu.com",
"Google": "www.google.cn "};
// Interactive use example
Userinfo = prompt ("enter the search engine you most want to go to: (Yahoo | Baidu | Google)", "Yahoo ");
Document. write ("your choice:" + userinfo + ", <a href = http: //" + geturl (userinfo) + "target = _ blank>" + "Click here to enter" + "</a>" + userinfo + ". ");
// geturl
// If the parameter is not defined, www.yahoo.cn is returned by default.
// @ Param choice select the name
// @ return URL actual URL
function geturl (choice) {
url = urlhash [choice];
If (typeof (urlhash [choice]) =" undefined ")
url =" www.yahoo.cn ";
return URL;
}
// Obtain all the keys in the hash list
// @ Param Hash hash Array
// @ Return keys key name data
Function array_keys (hash ){
Keys = [];
For (key in hash)
Keys. Push (key );
Return keys;
}
</SCRIPT>
2. Normal Array
Syntax for creating an array object:
New array (); new array (Size); New array (Element0,Element1,...,Elementn);
End