What are the uses of javascript associative arrays
This article mainly introduces the use of javascript associative array, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Hash associative array definition
The code is as follows
/ / define an empty array
Myhash = {}
/ / define the array directly
Myhash = {"key1": "val1", "key2": "val2"}
/ / define arrays with Array
Myhash = new Array ()
Myhash ["key1"] = "val1"
Myhash ["key2"] = "val2"
Add key values to the Hash associative array
The code is as follows
/ / add a new key newkey with a key value of newval
Myhash ["newkey"] = "newval"
Delete existing key values of Hash associative array
The code is as follows
/ / Delete a key newkey, and at the same time, the newval corresponding to that key value disappears.
Delete myhash ["newkey"]
Traversing Hash associative arrays
The code copies the code as follows
/ / traverse the entire hash array
For (key in myhash) {
Val = myhash [key]
}
An example of easy use of Hash associative arrays
The code is as follows
Urlhash = {"yahoo": "www.111cn.net"
"baidu": "www.baidu.com"
"google": "www.google.cn"}
/ / Interactive usage example
Userinfo = prompt ("enter the search engine you most want to go to: (yahoo | baidu | google)", "yahoo")
[xss_clean] ("your choice:" + userinfo + "," + "to enter" + "" + userinfo + ".")
/ / getURL
/ / if the parameter is not defined, the www.111cn.net URL is returned by default
/ / @ param choice Select name
/ / @ return url actual URL
Function getURL (choice) {
Url = urlhash [choice]
If (typeof (urlhash [choice]) = = "undefined")
Url = "www.111cn.net"
Return url
}
/ / get all the keys of 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
}
For example:
/ / for the traversal of associative arrays, first define an array:
The code is as follows
Var arr = new Array ()
/ / the data for casually creating an associative array is as follows:
Arr ["name"] = "mary"
Arr ["age"] = "3"
Arr ["sex"] = "man"
/ / use the for loop to traverse as follows:
For (var keyin arr)
{
/ / then the value taken by the above key variable is "name" or "age" or "sex"
/ / instead of the value of the array
/ / the value below is the corresponding value.
Var value = arr [key]
}
The properties of objects in JS can be parenthesized "[]" or "." To visit, for example, a ["a"] and a.an above are equivalent.
Examples
The following code creates and initializes an associative array of three elements (note the format):
The code is as follows
Var MyArray = {"a": "Athens", "b": "Belgrade", "c": "Cairo"}
In this array, you can use a string ("a", "b", or "c") to address the element, rather than the number (0, 1, or 2) of the array element.
This allows you to create and use arrays with a more intuitive addressing scheme. You can also use the for shown above. In statement code to traverse the array.
For (key in myArray)
[xss_clean] ("Element value is" + MyArray [key] + "
);
For example:
Var myArray = {"a": "Athens", "b": "Belgrade", "c": "Cairo"}
For (key in myArray) {
[xss_clean] (key+ "=" + myArray [key] + "
")
}
[xss_clean] ("a =" + myArray ["a"])
Thank you for reading this article carefully. I hope the article "what are the uses of javascript associative arrays" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!