• 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() < 1);
        };
        //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 >= 0 && 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 = 0, 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 = 0, 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 = 0, iLen = this.length(); i < iLen; i++) {
                    if (this.elements[i].key == key) {
                        this.elements.splice(i, 1);
                        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 = 0, 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 = 0, 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 = 0, iLen = this.length(); i < iLen; i++) {
                arr.push(this.elements[i].value);
            }
            return arr;
        }
    }
    View Code

    调用如下:

    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); });
  • 相关阅读:
    LR学习笔记6-常用术语
    LR学习笔记5-LR界面分析3
    LR学习笔记4-LR界面分析2
    LR学习笔记3-LR界面分析1
    Maven快速导出maven工程的依赖包
    Python3 将txt数据转换成列表,进行排序,筛选
    iOS OC环信实时语音切换听筒免提听不到声音报错:AVAudioSessionErrorCodeBadParam
    iOS voip电话和sip软电话 --网络电话
    远程(离线)推送自定义推送声音,友盟、极光
    Swift3.0 自定义tableView复用cell 的写法,与CollectionViewCell的不同,数据model
  • 原文地址:https://www.cnblogs.com/ysyn/p/6255289.html
Copyright © 2020-2023  润新知