js实现Dictionary
js是有Dictionary对象的,只是只有在IE浏览器下可以使用。
var dic = new ActiveXObject("Scripting.Dictionary");
但是在其它浏览器下,就需要js实现Dictionary:
var Dictionary=function() {
this.elements = new Array();
//Length of Dictionary
this.length = function () {
return this.elements.length;
};
//Check whether the Dictionary is empty
this.isEmpty = function () {
return (this.length() < );
};
//remove all elements from the Dictionary
this.removeAll = function () {
this.elements = new Array();
};
//get specify element of the dictionary
this.element = function (index) {
var rlt = null;
if (index >= && index < this.elements.length) {
rlt = this.elements[index];
}
return rlt;
}
//check whether the Dictionary contains this key
this.Exists = function (key) {
var rlt = false;
try {
for (var i = , iLen = this.length(); i < iLen; i++) {
if (this.elements[i].key == key) {
rlt = true;
break;
}
}
}
catch (ex) {
}
return rlt;
};
//check whether the Dictionary contains this value
this.containsValue = function (value) {
var rlt = false;
try {
for (var i = , iLen = this.length(); i < iLen; i++) {
if (this.elements[i].value == value) {
rlt = true;
break;
}
}
}
catch (ex) {
}
return rlt;
};
//remove this key from the Dictionary
this.remove = function (key) {
var rlt = false;
try {
for (var i = , iLen = this.length(); i < iLen; i++) {
if (this.elements[i].key == key) {
this.elements.splice(i, );
rlt = true;
break;
}
}
}
catch (ex) {
}
return rlt;
};
//add this key/value to the Dictionary,if key is exists,replace the value
this.add = function (key, value) {
this.remove(key);
this.elements.push({
key: key,
value: value
});
};
//add this key/value to the Dictionary,if key is exists,append value
this.set = function (key, value) {
var arr = this.getItem(key);
if (arr != null) {
if (typeof(arr) == "object") {
arr.unshift.apply(arr, value);
value = arr;
}
else {
var array = [];
array.push(arr);
array.unshift.apply(array, value);
value = array;
}
this.remove(key);
}
this.elements.push({
key: key,
value: value
});
}
//get value of the key
this.getItem = function (key) {
var rlt = null;
try {
for (var i = , iLen = this.length(); i < iLen; i++) {
if (this.elements[i].key == key) {
rlt = this.elements[i].value;
break;
}
}
}
catch (ex) {
}
return rlt;
};
//get all keys of the dictionary
this.keys = function () {
var arr = [];
for (var i = , iLen = this.length(); i < iLen; i++) {
arr.push(this.elements[i].key);
}
return arr;
}
//get all values of the dictionary
this.values = function () {
var arr = [];
for (var i = , iLen = this.length(); i < iLen; i++) {
arr.push(this.elements[i].value);
}
return arr;
}
}
调用如下:
var dicImg = new Dictionary(); dicImg.add(name, nameStr);
var isExist = dicImg.Exists(fs);
var dicValue = dicImg.getItem(arrKeys[i]);
遍历:
var arrkey = dicImg.keys();
$.each(arrkey, function (i, fe) {
var key = fe;
var value = dicImg.getItem(fe); });
js实现Dictionary的更多相关文章
- JS字典 Dictionary类
字典 Dictionary类 /*字典 Dictionary类*/ function Dictionary() { this.add = add; this.datastore = new Array ...
- JS模拟Dictionary
function Map() { this.keys = new Array(); this.data = new Array(); //添加键值对 this.set = function (key, ...
- Dictionary解析json,里面的数组放进list,并绑定到DataGridView指定列
Dictionary解析json,1.根据json建立相应的实体类,json里面的数组形式放进list集合2.取list中的数据,将相应的数据绑定到DataGridView,如下:循环(动态添加一行数 ...
- C#解析复杂的Json成Dictionary<key,value>并保存到数据库(多方法解析Json 四)
准备工作: 1.添加引用System.Web.Extensions, 2..net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Ass ...
- js中的字典
最近项目JS中需要建一个特殊的颜色库,需要用到类似C#中的dictionary的概念 然后一查发现JS没有dictionary 而是Array 初始化Array colorDic = new Arra ...
- convert number or string to ASCII in js
convert number or string to ASCII in js ASCII dictionary generator // const dict = `abcdefghijklmnop ...
- json字符串转泛型集合对象
Dictionary<string, object> jd = js.Deserialize<Dictionary<string, object>>(item); ...
- grunt安装与配置
安装 CLI npm install -g grunt-cli//全局安装 npm init //初始化package.json npm init 命令会创建一个基本的package.json文件 ...
- C# JSon转换
1. 先添加System.Web.Extensions.dll引用 var js = new System.Web.Script.Serialization.JavaScriptSerialize ...
随机推荐
- 微信扫描打开APP下载链接提示代码优化(转)
上一次我发了一篇文章叫“微信打开网址添加在浏览器中打开提示”,里面我发出来了三个代码,分别是纯JS.js+html.jQuery+HTML代码.今天来一个简化版带可以关闭的按钮操作.使用的是纯JS+H ...
- 首师大附中互测题:50229234海岛帝国:独立之战【C002】
[C002]50229234海岛帝国:独立之战[难度C]———————————————————————————————————————————————————————————————————————— ...
- JavaScript使用接口
在经典的Java面向对象语言中,可以用关键字interface来定义接口,用implement来实现接口,而JavaScript虽然也是面向对象语言,但是它并没有内置这些,不过由于JavaScript ...
- [Python学习] python 科学计算库NumPy—tile函数
在学习knn分类算法的过程中用到了tile函数,有诸多的不理解,记录下来此函数的用法. 函数原型:numpy.tile(A,reps) #简单理解是此函数将A进行重复输出 其中A和reps都是ar ...
- HDU 3308 LCIS(线段树)
题目链接 模板题吧,忘了好多,终于A了... #include <cstring> #include <cstdio> #include <string> #inc ...
- 【Linux】unzip命令,记一次遇到的问题
最近在做BOSS系统云平台部署脚本,联调时发现Shell脚本中存在问题,下方记录 某个地方提示是否覆盖 [root@haiwai test]# unzip /home/redis/test/main- ...
- ZeroMQ接口函数之 :zmq_ctx_new – 创建一个新的ZMQ 环境上下文
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_new zmq_ctx_new(3) ØMQ Manual - ØMQ/3.2 ...
- android studio 问题1
在使用androidStudio中,经常导入其他的项目,有时候会出现以下错误: rror:FAILURE: Build failed with an exception. * What went wr ...
- HTML当中特殊字符的表示
(回车换行) <br> (空格符) &(AND符号) & <(左尖括号.小于号) < >(右尖括号.大于号) > °(度) ° •(间隔符) ...
- HDU 1281 二分图
棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...