Javascript中数组与字典(即map)的使用

时间:2023-03-09 19:55:50
Javascript中数组与字典(即map)的使用

简述:

简单记录一下数据结构Map和数组,

其实在Javascript这种弱类型的脚本语言中,数组同时也就是字典,下面主要就是字典数组的简易使用

 

代码:

1. 数组中添加map

  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>

输出0:

Javascript中数组与字典(即map)的使用

2. 数组遍历输出

  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>

输出1:

Javascript中数组与字典(即map)的使用

3. 类似字典(map)遍历

  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>


输出2:

Javascript中数组与字典(即map)的使用

4. 字典声明时赋值

  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" : 20,
  11. "Jimmy" : 30
  12. };
  13. for(var key in dict)
  14. document.write(key + ": " + dict[key] + "</br>");
  15. </script>
  16. </body>
  17. </html>

输出3:

Javascript中数组与字典(即map)的使用

5.字典中嵌套数组

  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", "English"]
  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>


输:4:

Javascript中数组与字典(即map)的使用

6. 字典里value为数组, 数组内为字典,

下面的逻辑就是学生 :  课程列表 : 某门的课程信息

  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. {"English": 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>


输出5:

Javascript中数组与字典(即map)的使用

http://blog.****.net/anialy/article/details/8295765

相关文章