• js对Map的操作(可作为工具类)


    function Map() {   
        /** 存放键的数组(遍历用到) */  
        this.keys = new Array();   
        /** 存放数据 */  
        this.data = new Object();   
        /**  
         * 放入一个键值对  
         * @param {String} key  
         * @param {Object} value  
         */  
        this.put = function(key, value) {   
            if(this.data[key] == null){   
                this.keys.push(key);   
            }   
            this.data[key] = value;   
        };   
        /**  
         * 获取某键对应的值  
         * @param {String} key  
         * @return {Object} value  
         */  
        this.get = function(key) {   
            return this.data[key];   
        };   
        /**  
         * 删除一个键值对  
         * @param {String} key  
         */  
        this.remove = function(key) {   
            this.keys.remove(key);   
            this.data[key] = null;   
        };   
        /**  
         * 遍历Map,执行处理函数  
         *   
         * @param {Function} 回调函数 function(key,value,index){..}  
         */  
        this.each = function(fn){   
            if(typeof fn != 'function'){   
                return;   
            }   
            var len = this.keys.length;   
            for(var i=0;i<len;i++){   
                var k = this.keys[i];   
                fn(k,this.data[k],i);   
            }   
        };   
        /**  
         * 获取键值数组(类似Java的entrySet())  
         * @return 键值对象{key,value}的数组  
         */  
        this.entrys = function() {   
            var len = this.keys.length;   
            var entrys = new Array(len);   
            for (var i = 0; i < len; i++) {   
                entrys[i] = {   
                    key : this.keys[i],   
                    value : this.data[i]   
                };   
            }   
            return entrys;   
        };   
        /**  
         * 判断Map是否为空  
         */  
        this.isEmpty = function() {   
            return this.keys.length == 0;   
        };   
        /**  
         * 获取键值对数量  
         */  
        this.size = function(){   
            return this.keys.length;   
        };   
        /**  
         * 重写toString   
         */  
        this.toString = function(){   
            var s = "{";   
            for(var i=0;i<this.keys.length;i++,s+=','){   
                var k = this.keys[i];   
                s += k+"="+this.data[k];   
            }   
            s+="}";   
            return s;   
        };   
    }   
    //测试效果
    function testMap(){
    var m = new Map(); m.put('key1','Comtop'); m.put('key2','测试2'); m.put('key3','测试3'); alert("init:"+m); m.put('key1','测试测试'); alert("set key1:"+m); m.remove("key2"); alert("remove key2: "+m); var s =""; m.each(function(key,value,index){ s += index+":"+ key+"="+value+"/n"; }); alert(s); }
  • 相关阅读:
    python的生成Jwt
    qq邮箱验证
    DJango反序列化器的参数效验
    python三元运算,继承,help函数 args
    python时间板块,计算取值,math函数
    No migrations to apply. django同步数据库失败
    redis理论部分
    Java入门——day1
    HBO《硅谷》中的二进制码
    复习总表现(每天记录一下)
  • 原文地址:https://www.cnblogs.com/yydxh/p/15136290.html
Copyright © 2020-2023  润新知