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. Handlebars块级Helpers

    1.Handlebars简单介绍: Handlebars是JavaScript一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less templat ...

  2. 几年前做家教写的C教程(之四专讲了指针与汉诺塔问题)

    C语言学习宝典(4) 指针:可以有效的表示复杂的数据结构,能动态的分配动态空间,方便的使用字符串,有效的使用数组,能直接处理内存单元 不掌握指针就没有掌握C语言的精华 地址:系统为每一个变量分配一个内 ...

  3. wp8 入门到精通 仿美拍评论黑白列表思路

    static bool isbool = false; private void BindGameDelete() { Tile tile = new Tile(); List<Color> ...

  4. 数据的平面拟合 Plane Fitting

    数据的平面拟合 Plane Fitting 看到了一些利用Matlab的平面拟合程序 http://www.ilovematlab.cn/thread-220252-1-1.html

  5. ContentControl与ContentPresenter区别?

    <TextBlock HorizontalAlignment="Left" Text="{Binding HwContent, Converter={StaticR ...

  6. MySQL-多条件拼接语句

    BEGIN "; SET @_where=""; THEN SET @_where= CONCAT(@_where," AND sourcedomain=\&q ...

  7. 利用React实现表头维度功能

    这是我真正意义上地用react实现一些东西.这次分享的是一个很简单的小组件,效果图先放上来: 前端样式用的是一套框架.功能很简单,就是根据选择的维度,在成员里选择对应这个维度的选项. 首先初始化一些数 ...

  8. 解决scrollview上的menu拖动问题以及menu item在可视区外仍能触发的问题

    最近在做项目发现一个让人很头疼的问题 qiick-3.5 引擎 lua 版本 一 问题如下: ① 在Cocostudio中做界面 使用 scrollview 控件 ,然后 scrollview 控件的 ...

  9. 20145215实验二 Java面向对象程序设计

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...

  10. Oracle ORA-01722: 无效数字 处理方法

    C# + Oralce 10G 项目中 有用参数处理Update语句.参数命名和表字段同名.执行报错: ORA-01722: 无效数字 后修改所有的参数和对应字段不同.解决. 修改前: StringB ...