zoukankan      html  css  js  c++  java
  • 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); });
  • 相关阅读:
    css实现图像边框的样式
    css3 实现div靠右对齐
    将div水平,垂直居中的方式
    使用vue-cli可视化的方式创建项目后如何关闭ESLint代码检测
    清楚html和css标签自带默认样式
    vue动态请求到的多重数组循环遍历,取值问题,如果某个值存在则显示,不存在则不显示。
    python 编程
    python 错题集
    python+selenium页面自动化 元素定位实际遇到的各种问题(持续更新)
    使用Fiddle抓取IOS手机
  • 原文地址:https://www.cnblogs.com/ysyn/p/6255289.html
Copyright © 2011-2022 走看看