简述:
简单记录一下数据结构Map和数组,
其实在Javascript这种弱类型的脚本语言中,数组同时也就是字典,下面主要就是字典数组的简易使用
代码:
1. 数组中添加map
- <!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>
2. 数组遍历输出
- <!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>
输出1:
3. 类似字典(map)遍历
- <!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>
输出2:
4. 字典声明时赋值
- <!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" : 20,
- "Jimmy" : 30
- };
- for(var key in dict)
- document.write(key + ": " + dict[key] + "</br>");
- </script>
- </body>
- </html>
输出3:
5.字典中嵌套数组
- <!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", "English"]
- };
- var name = "Jeremy";
- for(var courseIndex in dict[name])
- document.write(dict[name][courseIndex] + "</br>");
- </script>
- </body>
- </html>
输:4:
6. 字典里value为数组, 数组内为字典,
下面的逻辑就是学生 : 课程列表 : 某门的课程信息
- <!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},
- {"English": 5}
- ];
- dict['Jimmy'] = courseListOfJimmy;
- document.write("Jimmy's Course Number Of Chinese: " + dict['Jeremy'][0]['Chinese']);
- </script>
- </body>
- </html>
输出5:
http://blog.csdn.net/anialy/article/details/8295765