Briefly:
Simply record the data structure map and array,
In fact, in JavaScript, the weak type of scripting language, the array is also a dictionary, the following is mainly the simple use of dictionary arrays
Code:
1. Add a map to the array
[HTML]View Plaincopy
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
- <title>test</title>
- <script type="Text/javascript">
- var arr = [];
- var key = ' Jeremy ';
- var value = '!!!! '
- Arr.push ({
- ' Key ': Key,
- ' Value ': value,
- });
- document.write ("key:" + arr[0][' key ') +
- "<BR/>value:" + arr[0][' value ']);
- </Script>
- </head>
- <body>
- </body>
- </html>
Output 0:
2. Array traversal output
[HTML]View Plaincopy
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
- <title>test</title>
- </head>
- <body>
- <script type="Text/javascript">
- var arr = [];
- Arr.push ("Jeremy");
- Arr.push ("Jimmy");
- for (var i in arr)
- document.write (i + ":" + arr[i] + "</br>");
- </Script>
- </body>
- </html>
Output 1:
3. Similar dictionary (map) traversal
[HTML]View Plaincopy
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
- <title>test</title>
- </head>
- <body>
- <script type="Text/javascript">
- var dict = [];//or dict = new Array ()
- dict["Jeremy"] = 20;
- dict["Jimmy"] = 30;
- for (var key in Dict)
- document.write (key + ":" + Dict[key] + "</br>");
- </Script>
- </body>
- </html>
Output 2:
4. Assigning values when a dictionary is declared
[Java]View Plaincopy
- <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c1>
- <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
- <title>Test</title>
- <body>
- <script type="Text/javascript" >
- var dict = {
- "Jeremy":
- "Jimmy":
- };
- for (var key in Dict)
- document.write (key + ":" + Dict[key] + "</br>");
- </script>
- </body>
Output 3:
5. Nested Arrays in dictionaries
[HTML]View Plaincopy
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
- <title>test</title>
- </head>
- <body>
- <script type="Text/javascript">
- var dict = {
- "Jeremy": ["Chinese", "Math"],
- "Jimmy": ["Art", "中文版"]
- };
- var name = "Jeremy";
- for (Var courseindex in Dict[name])
- document.write (Dict[name][courseindex] + "</br>");
- </Script>
- </body>
- </html>
Loss: 4:
6. The value in the dictionary is an array, which is a dictionary.
The following logic is the student: Course List: Course information for a certain door
[HTML]View Plaincopy
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
- <title>test</title>
- </head>
- <body>
- <script type="Text/javascript">
- var dict = [];
- var courselistofjeremy = [
- {"Chinese": 3},
- {"Math": 5}
- ];
- dict[' Jeremy '] = Courselistofjeremy;
- var courselistofjimmy = [
- {"Art": 3},
- {"中文版": 5}
- ];
- dict[' Jimmy ' = courselistofjimmy;
- document.write ("Jimmy's Course number of Chinese:" + dict[' Jeremy '][0][' Chinese ']);
- </Script>
- </body>
- </html>
Output 5:
[to] the use of arrays and dictionaries (that is, map) in JavaScript