Javascript中数组与字典(即map)的使用
简述:
简单记录一下数据结构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>
输出0:
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
Javascript中数组与字典(即map)的使用的更多相关文章
- [转] Javascript中数组与字典(即object)的使用
简述: 简单记录一下数据结构字典和数组, 其实在Javascript这种弱类型的脚本语言中,数组同时也就是字典,下面主要就是字典数组的简易使用 代码: 1. 数组中添加map <!DOCTYPE ...
- javascript中数组Array的方法
一.常用方法(push,pop,unshift,shift,join)push pop栈方法,后进先出var a =[1,2,3];console.log(a.push(40)); //4 返回数组的 ...
- javascript中数组的常用算法深入分析
Array数组是Javascript构成的一个重要的部分,它可以用来存储字符串.对象.函数.Number,它是非常强大的.因此深入了解Array是前端必修的功课.本文将给大家详细介绍了javascri ...
- 总结Javascript中数组各种去重的方法
相信大家都知道网上关于Javascript中数组去重的方法很多,这篇文章给大家总结Javascript中数组各种去重的方法,相信本文对大家学习和使用Javascript具有一定的参考借鉴价值,有需要的 ...
- 前端面试之JavaScript中数组的方法!【残缺版!!】
前端面试之JavaScript中数组常用的方法 7 join Array.join()方法将数组中所有元素都转化为字符串并连接在-起,返回最后生成的字 符串.可以指定一个可选的字符串在生成的字符串中来 ...
- JavaScript中数组操作常用方法
JavaScript中数组操作常用方法 1.检测数组 1)检测对象是否为数组,使用instanceof 操作符 if(value instanceof Array) { //对数组执行某些操作 } 2 ...
- Javascript中数组
Javascript中数组 1.什么是数组 所谓的数组就是一组数据的集合,在内存中表现为一段连续的内存地址(保存在堆内存) 2.创建数组的含义 创建数组的目的:就是为了保存更多的数据 3.数组的定义 ...
- javascript中数组常用方法总结
原文:javascript中数组常用方法总结 在javascript的基础编程中,数组是我们最常遇到的,那么数组的一些常用方法也是我们必须要掌握的,下面我们总结一下数组中常用的方法. toString ...
- JavaScript中数组Array方法详解
ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...
随机推荐
- ad画fpc
得到新技能 后记: 实际情况,复杂很多pin的fpc 都是用cad画的.我明天学...
- WebGL如何解决中文文字载入
关于WebGL载入中文字体问题,我在网上搜了一下,发现例子并不多,而且只能实现隶书的载入,不支持其他中文字体. 下面是实现的代码: <script src="../js/three.m ...
- Cannot forward after response has been committed
项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...
- maya的卡通渲染
为了统一工作流,给maya也做了个卡通shader:
- ThinkPHP_基础(1)目录结构
(说明:文中的颜色一一对应) 目录结构 www WEB部署目录(或者子目录) ├─index.php 入口文件 ├─README.md README文件 ├─composer.json Compose ...
- SqlServer性能优化 通过压缩与计算列提高性能(十一)
压缩: 1.压缩的对象 1.表 2.索引(非聚集索引手工做) 3.备份(手工做) 2.对性能影响 1.提高IO性能 2.降低CPU性能 行压缩: 1.对null值不占用空间 2.对Nu ...
- HDU 5015
http://acm.hdu.edu.cn/showproblem.php?pid=5015 矩阵是表示状态转移的利器 这题m很大,n非常小,所以开始的思考角度是能否从当前列推出下一列.有了这个角度, ...
- python 获取html源代码里标签之间的文本用get_text()
例: 输出<span class="w-txt">分享</span>中的文本"分享" contents = bsObj.find_all ...
- MyEclipse使用前优化与配置
全局优化 1 设置默认编码方式 首选项> General > Workspace > GBK改成UTF-8 2 设置默认文件默认打开方式 首选项> General > ...
- 信号量 semaphore 和 @synchronized 的运用
1. //创建全局队列 dispatch_queue_t queue = dispatch_get_global_queue(0, 0); //创建信号量 dispatch_semaphore_t s ...