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()方 ...
随机推荐
- xlwt写入中文操作不成功,提示UnicodeDecodeError: ascii codec can't decode byte ...
打开xlwt包里的Workbook.py文件,修改Workbook类的__init__方法 将 def __init__(self, encoding='ascii', style_compressi ...
- Git 的深入理解与GitHub托管服务(转)
源代码管理系统(SCM)与版本控制 版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统. 本地版本控制系统 许多人习惯用复制整个项目目录的方式来保存不同的版本,或许还会 ...
- Android TextView设置多彩文字
在应用开发中时常会遇到需要在一段文字中插入几个不一样颜色文字的需求; 以前本人都是用多个TextView拼起来,不仅感觉很蠢,操作起来也蛮恶心; 直到接触到了SpannableStringBuilde ...
- iOS内置音频
Predefined soundsThere are some predefined system sounds, for the system sound ID in the range 1000 ...
- 解决JS中各浏览器Date格式不兼容的问题
IE,Chrome和FireFox等浏览器都支持的一种日期格式是:2015/11/30 19:29:23. 所以,可以这样写: var timeStr = new Date("2015/11 ...
- win7 64位安装pygame
需要的工具包 Python安装包 Pip安装包(版本无要求) Pygame安装包(版本需要与python匹配) http://jingyan.baidu.com/article/425e69e6ed3 ...
- MAC下如何显示隐藏文件
1.在终端上输入以下命令 defaults write com.apple.finder AppleShowAllFiles -bool true 2.重新启动Finder Command + Opt ...
- div中显示页面
在css中显示页面,在页面布局中很多时候都要在一个div 里显示某些页面.在这里写下我用到的一种方式. <script type="text/javascript"> ...
- IOS一些高效的第三方框架库
MBProgressHUD ——进展指示符库 苹果的应用程序一般都会用一种优雅的,半透明的进度显示效果,不过这个API是不公开的,因此你要是用了,很可能被清除出AppStore.而 MBProgres ...
- linux精确查找命令
1. find命令 命令 功能:搜寻文件与目录 功能: 语法: 语法:find 目录名 选项 常用选项有: 常用选项有: -name filename按名字查找 按名字查找 -type x 查找类型为 ...