javascript字典数据结构常用功能实现
必知必会啊。
function Dictionary(){
var items = {};
this.has = function (key) {
return key in items;
};
this.set = function(key, value){
items[key] = value;
};
this.remove = function(key){
if (this.has(key)){
delete items[key];
return true;
}
return false;
};
this.get = function(key){
return this.has(key) ? items[key] : undefined;
};
this.values = function(){
var values = [];
for(var k in items){
if (this.has(k)) {
values.push(items[k]);
}
}
return values;
};
this.clear = function(){
items = {};
};
this.size = function(){
var count = 0;
for (var prop in items){
if(items.hasOwnProperty(prop)){
++count;
}
}
return count;
};
this.getItems = function(){
return items;
};
}
var dictionary = new Dictionary();
dictionary.set('Gandalf', 'gandalf@email.com');
dictionary.set('John', 'johnsnow@email.com');
dictionary.set('Tyrion', 'tyrion@email.com');
console.log(dictionary.has('Gandalf'));
console.log(dictionary.size());
//console.log(dictionary.keys());
console.log(dictionary.values());
console.log(dictionary.get('Tyrion'));
dictionary.remove('John');
console.log(dictionary.values());
console.log(dictionary.get('Tyrion'));

javascript字典数据结构常用功能实现的更多相关文章
- javascript字典数据结构Dictionary实现
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...
- JavaScript之Array常用函数汇总
[20141121]JavaScript之Array常用功能汇总 *:first-child { margin-top: 0 !important; } body>*:last-child { ...
- JavaScript 常用功能总结
小编吐血整理加上翻译,太辛苦了~求赞! 本文主要总结了JavaScript 常用功能总结,如一些常用的JS 对象,基本数据结构,功能函数等,还有一些常用的设计模式. 目录: 众所周知,JavaScri ...
- 字典--数据结构与算法JavaScript描述(7)
字典 字典是一种以键-值对形式存储数据的数据结构. Dictionary 类 Dictionary 类的基础是Array 类,而不是Object 类. function Dictionary( ){ ...
- C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法
C#构造方法(函数) 一.概括 1.通常创建一个对象的方法如图: 通过 Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...
- WebStorm 常用功能的使用技巧分享
WebStorm 是 JetBrain 公司开发的一款 JavaScript IDE,使用非常方便,可以使编写代码过程更加流畅. 本文在这里分享一些常用功能的使用技巧,希望能帮助大家更好的使用这款强大 ...
- [转]WebPack 常用功能介绍
概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...
- WebPack常用功能介绍
概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...
- 从零开始学习jQuery (十) jQueryUI常用功能实战
一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案, 即使你会使用jQuery也能在阅读中发现些许秘籍. 本文是实战篇. 使用jQueryUI完成制作网站 ...
随机推荐
- Developing a plugin framework in ASP.NET MVC with medium trust
http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx January 7, ...
- js实现鼠标点击input框后里面的内容就消失代码
<!--# <a href="http://www.mianfeimoban.com/texiao_mb/" target="_blank" cla ...
- C语言的一点操作(学习笔记)
#include <stdio.h> #define LENTEST 100 // 采取逐步删除的方法求的素数 //先假设1-100都是素数,然后剔除2的倍数, //3的倍数,直到剔除所有 ...
- Linux 解压命令tar的理解
今天回顾了下tar 这个打包工具的一些常用参数 选项与参数: -c :创建打包文件,可搭配 -v 来察看过程中被打包的档名(filename) -t :察看打包文件的内容含有哪些档名,重点在察看『档名 ...
- icon@font-face那些事
http://files.cnblogs.com/files/LoveOrHate/font.rar @font-face { font-family: 'Glyphicons Regular'; s ...
- GNU make 升级
网上下载新版本的make文件后 在make目录下 ./configure make make install mv make /opt/mv_pro_5.0.0/montavista/common/b ...
- 关于Tabula FPGA的话题 (转)
最近关注了一下Tabula的产品,特别是这个公司的FPGA.我的关注源于Intel对Tabula的支持.最近和在FPGA方面有较深理解的几位人士简单聊了聊,以下是这次对话的主要内容,希望能对从事这方面 ...
- vsPhere安装虚拟sm
1.在机器上单击右键 2.选择“编辑设置” 设备状态,选择打开电源时链接,数据存储ISO文件,选择镜象. 3.重启,进入安装界面. 4.
- 用Prime31实现Google Play In-App-Blling
Android开发者想在海外赚钱,接入Google Play是不二选择,然而一堆英文文档对于像我这样的英语四级都木有过的可谓是苦恼之极.近段时间因工作需要研究了Unity接入Google Play的整 ...
- change column to bigint
今天存储数据的时候报错,发现是3435065640超出了常规int的存储长度, RangeError (3435065640 is out of range for ActiveRecord::Typ ...