JavaScript 字典(Dictionary)
TypeScript方式实现源码
// set(key,value):向字典中添加新元素。
// remove(key):通过使用键值来从字典中移除键值对应的数据值。
// has(key):如果某个键值存在于这个字典中,则返回true,反之则返回false。
// get(key):通过键值查找特定的数值并返回。
// clear():将这个字典中的所有元素全部删除。
// size():返回字典所包含元素的数量。与数组的length属性类似。
// keys():将字典所包含的所有键名以数组形式返回。
// values():将字典所包含的所有数值以数组形式返回。
/**
* 字典
* @desc 与Set类相似,ECMAScript 6同样包含了一个Map类的实现,即我们所说的字典
*/
class Dictionary {
private items = {};
public set(key, value) {
this.items[key] = value;
}
public remove(key) {
if (this.has[key]) {
delete this.items[key];
return true;
} else {
return false;
}
}
public has(key) {
return key in this.items;
}
public get(key) {
return this.has(key) ? this.items[key] : undefined;
}
public clear() {
this.items = {};
}
public size() {
var count = ;
for (var prop in this.items) { //{5}
if (this.items.hasOwnProperty(prop)) //{6}
++count; //{7}
}
return count;
}
public keys() {
let values = [];
for (var k in this.items) {
if (this.has(k)) {
values.push(k);
}
}
return values;
}
public values(): Array<any> {
let values = [];
for (var k in this.items) {
if (this.has(k)) {
values.push(this.items[k]);
}
}
return values;
}
public getItems() {
return this.items;
}
}
字典 Dictionary
// 使用我们创建的类来执行如下代码:
var dictionary = new Dictionary();
dictionary.set('Gandalf', 'gandalf@email.com');
dictionary.set('John', 'johnsnow@email.com');
dictionary.set('Tyrion', 'tyrion@email.com');
// 如果执行了如下代码,输出结果将会是true:
console.log(dictionary.has('Gandalf'));
// 下面的代码将会输出3,因为我们向字典实例中添加了三个元素:
console.log(dictionary.size());
// 现在,执行下面的几行代码:
console.log(dictionary.keys());
console.log(dictionary.values());
console.log(dictionary.get('Tyrion'));
// 输出结果分别如下所示:
// ["Gandalf", "John", "Tyrion"]
// ["gandalf@email.com", "johnsnow@email.com", "tyrion@email.com"]
// tyrion@email.com
// 最后,再执行几行代码:
dictionary.remove('John');
// 再执行下面的代码:
console.log(dictionary.keys());
console.log(dictionary.values());
console.log(dictionary.getItems());
// 输出结果如下所示:
// ["Gandalf", "Tyrion"]
// ["gandalf@email.com", "tyrion@email.com"]
// Object {Gandalf: "gandalf@email.com", Tyrion: "tyrion@email.com"}
// 移除了一个元素后, 现在的dictionary实例中只包含两个元素了。 加粗的一行表现了items
// 对象的内部结构。
使用方式
JavaScript方式实现源码
/**
* 字典
* @desc 与Set类相似,ECMAScript 6同样包含了一个Map类的实现,即我们所说的字典
*/
var Dictionary = (function () {
function Dictionary() {
this.items = {};
}
Dictionary.prototype.set = function (key, value) {
this.items[key] = value;
};
Dictionary.prototype.remove = function (key) {
if (this.has[key]) {
delete this.items[key];
return true;
}
else {
return false;
}
};
Dictionary.prototype.has = function (key) {
return key in this.items;
};
Dictionary.prototype.get = function (key) {
return this.has(key) ? this.items[key] : undefined;
};
Dictionary.prototype.clear = function () {
this.items = {};
};
Dictionary.prototype.size = function () {
var count = ;
for (var prop in this.items) {
if (this.items.hasOwnProperty(prop))
++count; //{7}
}
return count;
};
Dictionary.prototype.keys = function () {
var values = [];
for (var k in this.items) {
if (this.has(k)) {
values.push(k);
}
}
return values;
};
Dictionary.prototype.values = function () {
var values = [];
for (var k in this.items) {
if (this.has(k)) {
values.push(this.items[k]);
}
}
return values;
};
Dictionary.prototype.getItems = function () {
return this.items;
};
return Dictionary;
}());
字典 Dictionary
JavaScript 字典(Dictionary)的更多相关文章
- javascript字典数据结构Dictionary实现
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...
- 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密
你真的了解字典(Dictionary)吗? 从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...
- C#创建安全的字典(Dictionary)存储结构
在上面介绍过栈(Stack)的存储结构,接下来介绍另一种存储结构字典(Dictionary). 字典(Dictionary)里面的每一个元素都是一个键值对(由二个元素组成:键和值) 键必须是唯一的,而 ...
- 索引器、哈希表Hashtabl、字典Dictionary(转)
一.索引器 索引器类似于属性,不同之处在于它们的get访问器采用参数.要声明类或结构上的索引器,使用this关键字. 示例: 索引器示例代码 /// <summary> /// 存储星 ...
- Python 字典(Dictionary)操作详解
Python 字典(Dictionary)的详细操作方法. Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型. 一.创建字典 字典由键和对应值成对组成.字 ...
- Python 字典(Dictionary) get()方法
描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...
- Python字典 (dictionary)
字典dict,是Python唯一的标准mapping类型,也是内置在Python解释器中的. mapping object把一个可哈希的值(hashable value)映射到一个任意的object上 ...
- Python 字典(Dictionary) setdefault()方法
描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...
- C#字典Dictionary排序(顺序、倒序)
这里是针对.NET版本过低的排序方式,没怎么用过,记录一下: 一.创建字典Dictionary 对象 假如 Dictionary 中保存的是一个网站页面流量,key 是网页名称,值value对应的是网 ...
随机推荐
- leaflet简单例子,绘制多边形
var crs = L.CRS.EPSG900913; var map = L.map('map', { crs: crs, width: '100%', height: '100%', maxZoo ...
- 本地不安装oracle,PLsql远程连接
Oracle的Instant client工具包可以很好地解决本地不安装oracle,PLsql远程连接. 1.首先到Oracle网站下载Instant Client : http://www.ora ...
- CentOS 6.5 通过命令行安装发送邮件
1.安装sendmail: yum install sendmail 2.安装mailx: yum install mailx -y 3.编辑发送的配置文件: vi /etc/mail.rc #在最后 ...
- Notepad++中实现Markdown语法高亮与实时预览
Notepad ++是一个十分强大的编辑器,除了可以用来制作一般的纯文字说明文件,也十分适合编写计算机程序代码.Notepad ++不仅有语法高亮度显示,也有语法折叠功能,并且支持宏以及扩充基本功能的 ...
- winform 适配high dpi
在 mainifest文件中添加:(新建mainifest文件的时候以下内容是有的,只要取消注释就可以了) <compatibility xmlns="urn:schemas-micr ...
- C++中文件的读写
C++中文件的读写 在C++中如何实现文件的读写? 一.ASCII 输出 为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstrea ...
- Hibernate之深入Hibernate的映射文件
这周周末 要把hibernate的映射文件搞定 .. 1.映射文件的主结构 主要结构 :根元素为<hibernate-mapping ></hibernate-mapping> ...
- JAVA_SE基础——48.多态
面向对象程序设计的三个特点是封装.继承和多态.前面已经学习了前两个特点.本章节将介绍多态性. 多态:一个对象具备多种形态.(父类的引用类型变量指向了子类的对象)或者是接口 的引用类型变量指向了接口实现 ...
- proxymysql的安装与应用
具体的资料我们可以查看官方的文档:https://github.com/sysown/proxysql/wiki/ProxySQL-Configuration 推荐下载最新的Proxysql. 下面跟 ...
- python subprocess模块使用总结
一.subprocess以及常用的封装函数运行python的时候,我们都是在创建并运行一个进程.像Linux进程那样,一个进程可以fork一个子进程,并让这个子进程exec另外一个程序.在Python ...