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的更多相关文章

  1. JS字典 Dictionary类

    字典 Dictionary类 /*字典 Dictionary类*/ function Dictionary() { this.add = add; this.datastore = new Array ...

  2. JS模拟Dictionary

    function Map() { this.keys = new Array(); this.data = new Array(); //添加键值对 this.set = function (key, ...

  3. Dictionary解析json,里面的数组放进list,并绑定到DataGridView指定列

    Dictionary解析json,1.根据json建立相应的实体类,json里面的数组形式放进list集合2.取list中的数据,将相应的数据绑定到DataGridView,如下:循环(动态添加一行数 ...

  4. C#解析复杂的Json成Dictionary<key,value>并保存到数据库(多方法解析Json 四)

    准备工作: 1.添加引用System.Web.Extensions, 2..net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Ass ...

  5. js中的字典

    最近项目JS中需要建一个特殊的颜色库,需要用到类似C#中的dictionary的概念 然后一查发现JS没有dictionary 而是Array 初始化Array colorDic = new Arra ...

  6. convert number or string to ASCII in js

    convert number or string to ASCII in js ASCII dictionary generator // const dict = `abcdefghijklmnop ...

  7. json字符串转泛型集合对象

    Dictionary<string, object> jd = js.Deserialize<Dictionary<string, object>>(item); ...

  8. grunt安装与配置

    安装 CLI npm install -g grunt-cli//全局安装 npm init //初始化package.json npm init   命令会创建一个基本的package.json文件 ...

  9. C# JSon转换

    1. 先添加System.Web.Extensions.dll引用   var js = new System.Web.Script.Serialization.JavaScriptSerialize ...

随机推荐

  1. MyEclipse Project Migration功能中文简单介绍

    前端时间,我对myEclispe的project Migration产生了疑问,也不知道是干啥用的.然后百度之,翻译结果是项目迁移,再次百度其他人对这个的经验,没想到百度到的没多少,甚至都没有说明这个 ...

  2. ThinkPHP 表单提交操作成功后执行JS操作如何刷新父页面或关闭当前页等操作

    ThinkPHP 表单提交操作成功后执行JS操作如何刷新父页面或关闭当前页等操作 .操作成功后刷新父页面 $this->assign('jumpUrl', "javascript:wi ...

  3. Android_ListView简单例子

    ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件 activity_main.xm ...

  4. jquery.validate.js表单验证

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  5. div宽高设置为百分比

    如果你将div的width和height设置为百分比,但是发现页面都不见了,这是因为父标签也要设置为百分比,也就是说body和html的宽高也需要设置为百分比 #containter{ width:1 ...

  6. POJ 2386 题解

    Lake Counting 描述 Due to recent rains, water has pooled in various places in Farmer John's field, whi ...

  7. cetnos 7 ntp服务的安装与配置

    首先需要搭建yum本地仓库 http://www.cnblogs.com/jw35/p/5967677.html   #搭建yum仓库方法 yum install ntp -y        #安装n ...

  8. ImportError: The _imagingft C module is not installed

    添加验证码模块的时候,发布到服务器上居然报了这个错误 ImportError: The _imagingft C module is not installed 然而pillow是已经装在服务器上的, ...

  9. iostat命令学习

    iostat命令主要用于监控linux系统下cup和磁盘IO的统计信息 可以通过iostat --help获得该命令的帮助信息 [oracle@std ~]$ iostat --help Usage: ...

  10. php文件写入PHP_EOL与FILE_APPEND

    PHP_EOL 换行符 unix系列用 \n windows系列用 \r\n mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 FILE_APPEND  用于文本追加 ...