javascript Dictionary data structures
Dictionary常被称为数据字典,是一种用来保存键值对的数据结构,比如我们日常的电话本,就是一个Dictionary。我们通过键(名字),就可以访问到对应的值(电话号码)。在C++与java中我们都是使用map来构建这样一个Dictionary,只是名字不同而已。在javascript中对象就好像是一个Dictionary一样,因为对象就是一个属性名/属性值的集合。
为了更好的体现出Dictionary,我们可以基于Array与object来构建一个使用方便简单的Dictionary类:
Dictionary类:
<html>
<head>
<title>Date Example</title>
</head>
<body>
<div id="content"></div>
<input type="button" value="Set InnerHTML" onclick="testDate()"> <script type="text/javascript"> function Dictionary (){
this.dataArray = []; // 添加元素
this.add = function (key, item){
this.dataArray[key] = item;
}; // 查找元素
this.find = function (key){
return this.dataArray[key];
}; // 删除元素
this.remove = function (key){
delete this.dataArray[key];
}; // 显示元素
this.showAllItems = function (){
for (var key in Object.keys(this.dataArray)){
console.log(key + ":" + this.dataArray[key]);
}
} } </script>
</body>
</html>
除了上面这些方法之外,我们也可以根据自己的使用来添加其他的方法与属性。
// 删除字典中所有元素
this.clearAll = function (){
for (var key in Object.keys(this.dataArray)){
delete this.dataArray[key];
}
};
// 字典中的元素个数
this.itemsCount = function (){
var num = 0;
for (var key in Object.keys(this.dataArray)){
++num;
}
return num;
}
在itemsCount中查询数量为什么不直接使用this.dataArray.length呢???这是因为在键的类型为字符串类型的时候,length这个属性就不起什么作用了。。。。
字典的用途大多说时候我们之关心通过键开获取值,不会去关心键的顺序问题,但是为了更好的显示我们前面说的那个电话本,可以对电话本中的键(名字)进行排序。
在javascript中我们常见的是对数组进行排序,那利用数组的sort()进行排序如何呢,你会看到silently。主要原因就是我们平时使用的数组是在数组按照整型值的索引下对数组元素进行排序的,现在的数组的索引变成了字符串,这种形式的排序也就失去了效果。其实我们可以取出Dictionary中的键,把键放入一个数组A中,然后对这个数组A进行排序,然后利用数组A来遍历Dictionary。Object.keys()就提供了这样一种方法:
this.sequenceShow = function (){
for (var key in Object.keys(this.dataArray).sort()){
console.log(key + ":" + this.dataArray[key]);
}
}
这样我们就可以实现把“键”进行排序了。。。
javascript Dictionary data structures的更多相关文章
- javascript Set data structures
集合(set)是一组无序的,但彼此之间又有一定相关性的数据集.每个成员在数组中只能出现一次. 在使用集合(set)之前最好先理解一下内容: 1.不包含任何成员的集合称为空集合. 2.如果两个集合的成员 ...
- javascript linkedlist data structures
在使用C++的时候我们经常会使用到各种容器,这些容器其实就是一种数据结构.在java中其实也是如此.但是由于javascript只给我们提供了一种内置的数据结构数组,准备来说是对象.没有我们常见的那些 ...
- JavaScript data types and data structures
JavaScript data types and data structures Programming languages all have built-in data structures, b ...
- 《Python Data Structures》Week5 Dictionary 课堂笔记
Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week5 Dictionary 9.1 Dictionaries 字 ...
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
To demonstrate the difference between mutability and immutability, imagine taking a drink from a gla ...
- Go Data Structures: Interfaces
refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...
- 学习笔记之Problem Solving with Algorithms and Data Structures using Python
Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...
- [译]The Python Tutorial#5. Data Structures
[译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...
- A library of generic data structures
A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...
随机推荐
- Unity3d---> IEnumerator
Unity3d---> IEnumerator 2013-04-18 10:24 2162人阅读 评论(0) 收藏 举报 Unity3dc# using UnityEngine; using S ...
- C#.NET常见问题(FAQ)-interface接口如何理解
个人把interface理解为一种比较特殊的判断技巧,不是常规的变量类型比如判断字符串,判断数组,而是判断类的实例是否拥有某些属性或者方法(比如有十个女的穿一样的衣服,头上盖住,让新郎去猜哪一个是他的 ...
- Fusioncharts的导出图片訪问官网问题
Fusioncharts3.5使用自带的导出功能,须要訪问官网 问题描写叙述:使用fusioncharts自带的exportchart方法来导出图片的时候.要訪问export.api3.fusionc ...
- 将 Shiro 作为应用的权限基础 二:shiro 认证
认证就是验证用户身份的过程.在认证过程中,用户需要提交实体信息(Principals)和凭据信息(Credentials)以检验用户是否合法.最常见的“实体/凭证”组合便是“用户名/密码”组合. 一. ...
- okhttp进行网络传输文件
其中使用了RXJava. public class HttpDataManager { private static HttpDataManager INSTANCE; private Request ...
- java 如何查看jdk版本&位数
java 如何查看jdk版本&位数 CreateTime--2018年4月22日18:20:18 Author:Marydon 方式一:通过dos命令实现 win+R-->cmd-- ...
- eclipse javaWeb项目如何引入jar包
eclipse javaWeb项目如何引入jar包 CreateTime--2018年4月19日08:54:24 Author:Marydon 1.判断当前jar包是否已经引入到项目当中的3种方式 ...
- uva 699 The Falling Leaves(建二叉树同一时候求和)
本来看着挺难的.大概是由于我多瞟了一眼题解,瞬间认为简单多了.做题就得这样,多自己想想.如今是 多校联赛,然而我并不会做. .. .慢慢来,一直在努力. 分析: 题上说了做多不会超过80行.所以能够开 ...
- DIV+CSS布局重新学习之使用A标签和CSS制作按钮
这里主要利用A元素的伪类来实现: a:link {color: #FF0000} /* 未访问的链接 */ a:visited {color: #00FF00} /* 已访问的链接 */ a:hove ...
- jquery toastmessage (Jquery类似安卓消息提示框)
Do you wanna have some toasts ? jquery-toastmessage-plugin is a JQuery plugin which provides android ...