数据结构——字典(JavaScript)
来源:互联网 发布:数据库面试宝典 编辑:程序博客网 时间:2024/06/17 12:41
创建字典
function Dictionary(){//首先申明一个类 var items = {}; this.set = function(Key,value){//向字典中添加新元素 items[Key] = value; } this.remove = function(Key){//通过使用键值对来从字典中移除键值对应的数据值 if(Key in items){ delete items[Key]; return true; }else{ return false; } } this.has = function(Key){//如果某个键值存在于这个字典中,就返回true,否则返回false return Key in items; } this.get = function(Key){//通过键值查找指定的数值并返回 return this.has(Key) ? items[Key] : undefined; } this.clear = function(){//删除字典中所有的元素 items = {}; return true; } this.size = function(){//返回字典所包含元素的数量 return this.Keys().length; } this.Keys = function(){ var keys = []; for(var k in items){ if(this.has(k)){ keys.push(k); } } return keys; } this.values = function(){//将字典所包含的所有数值以数组形式返回 var values = []; for(var k in items){ if(this.has(k)){ values.push(items[k]); } } return values; } this.getItems = function(){ return items; }}
使用Dictionary类
var dictionary = new Dictionary();dictionary.set('Tom','123@qq.com');dictionary.set('Bob','456@163.com');dictionary.set('John','789@126.com');console.log(dictionary.has('Tom'));console.log(dictionary.size());console.log(dictionary.Keys());console.log(dictionary.values());console.log(dictionary.get('Bob'));console.log(dictionary.remove('John'));console.log(dictionary.size());console.log(dictionary.Keys());console.log(dictionary.values());console.log(dictionary.getItems());
控制台输出
阅读全文
0 0
- 数据结构——字典(JavaScript)
- 学习JavaScript数据结构与算法(六)——字典
- 数据结构:JavaScript实现字典
- JavaScript数据结构-字典
- javascript实现字典数据结构
- 数据结构与算法JavaScript - 字典
- javascript字典数据结构Dictionary实现
- Python数据结构:映射(map)——字典
- python基础知识——内置数据结构(字典)
- Python数据结构之——字典
- Python数据结构——序列、字典
- javascript数据结构6-字典 散列 集合
- 数据结构——数组(JavaScript)
- 数据结构——栈(JavaScript)
- 数据结构——队列(JavaScript)
- 数据结构——链表(JavaScript)
- 数据结构——集合(JavaScript)
- 数据结构——树(JavaScript)
- 全排列的C/C++实现
- JS--第八天
- java 泛型
- LeetCode-26-Remove Duplicates from Sorted Array(消除已排序数组中的重复元素)
- Nginx之location,rewrite,反向代理及负载均衡
- 数据结构——字典(JavaScript)
- Quick-cocos2dx,中关于table条件排序
- JAVA设计模式之单例模式
- 关于Java中的编程规约
- 【解决方案】display: box; 盒子布局无效的问题
- 关联查询过滤,过滤表A包含表B的数据
- yum Cannot find a valid baseurl for repo: base/7/x86_64
- LinkedHashMap和HashMap的比较使用
- 随笔2017-07-24