[to] the use of arrays and dictionaries (that is, map) in JavaScript

Source: Internet
Author: User

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
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  5. <title>test</title>
  6. <script type="Text/javascript">
  7. var arr = [];
  8. var key = ' Jeremy ';
  9. var value = '!!!! '
  10. Arr.push ({
  11. ' Key ': Key,
  12. ' Value ': value,
  13. });
  14. document.write ("key:" + arr[0][' key ') +
  15. "<BR/>value:" + arr[0][' value ']);
  16. </Script>
  17. </head>
  18. <body>
  19. </body>
  20. </html>

Output 0:


2. Array traversal output [HTML]View Plaincopy
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  5. <title>test</title>
  6. </head>
  7. <body>
  8. <script type="Text/javascript">
  9. var arr = [];
  10. Arr.push ("Jeremy");
  11. Arr.push ("Jimmy");
  12. for (var i in arr)
  13. document.write (i + ":" + arr[i] + "</br>");
  14. </Script>
  15. </body>
  16. </html>

Output 1:


3. Similar dictionary (map) traversal [HTML]View Plaincopy
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  5. <title>test</title>
  6. </head>
  7. <body>
  8. <script type="Text/javascript">
  9. var dict = [];//or dict = new Array ()
  10. dict["Jeremy"] = 20;
  11. dict["Jimmy"] = 30;
  12. for (var key in Dict)
  13. document.write (key + ":" + Dict[key] + "</br>");
  14. </Script>
  15. </body>
  16. </html>

Output 2:



4. Assigning values when a dictionary is declared
[Java]View Plaincopy
  1. <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > " /c1>
  2. <meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">
  3. <title>Test</title>
  4. <body>
  5. <script type="Text/javascript" >
  6. var dict = {
  7. "Jeremy":
  8. "Jimmy":
  9. };
  10. for (var key in Dict)
  11. document.write (key + ":" + Dict[key] + "</br>");
  12. </script>
  13. </body>

Output 3:


5. Nested Arrays in dictionaries [HTML]View Plaincopy
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  5. <title>test</title>
  6. </head>
  7. <body>
  8. <script type="Text/javascript">
  9. var dict = {
  10. "Jeremy": ["Chinese", "Math"],
  11. "Jimmy": ["Art", "中文版"]
  12. };
  13. var name = "Jeremy";
  14. for (Var courseindex in Dict[name])
  15. document.write (Dict[name][courseindex] + "</br>");
  16. </Script>
  17. </body>
  18. </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
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  5. <title>test</title>
  6. </head>
  7. <body>
  8. <script type="Text/javascript">
  9. var dict = [];
  10. var courselistofjeremy = [
  11. {"Chinese": 3},
  12. {"Math": 5}
  13. ];
  14. dict[' Jeremy '] = Courselistofjeremy;
  15. var courselistofjimmy = [
  16. {"Art": 3},
  17. {"中文版": 5}
  18. ];
  19. dict[' Jimmy ' = courselistofjimmy;
  20. document.write ("Jimmy's Course number of Chinese:" + dict[' Jeremy '][0][' Chinese ']);
  21. </Script>
  22. </body>
  23. </html>

Output 5:


[to] the use of arrays and dictionaries (that is, map) in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.