木头人的gravatar头像
木头人 2017-12-07 22:30:40
js实现hashmap存储数据
function HashMap(){
        this.map = {};
    }
    HashMap.prototype = {
        put : function(key , value){// 向Map中增加元素(key, value) 
            this.map[key] = value;
        },
        get : function(key){ //获取指定Key的元素值Value,失败返回Null 
            if(this.map.hasOwnProperty(key)){
                return this.map[key];
            }
            return null;
        },
        remove : function(key){ // 删除指定Key的元素,成功返回True,失败返回False
            if(this.map.hasOwnProperty(key)){
                return delete this.map[key];
            }
            return false;
        },
        removeAll : function(){  //清空HashMap所有元素
            this.map = {};
        },
        keySet : function(){ //获取Map中所有KEY的数组(Array) 
            var _keys = [];
            for(var i in this.map){
                _keys.push(i);
            }
            return _keys;
        }
    };
    HashMap.prototype.constructor = HashMap;

本来公司开始让我用localstorage存储,我看了下教程,localstorage存储在本地。

看了下这个地址https://github.com/flesler/hashmap/blob/master/hashmap.js写的挺好的


打赏
最近浏览
doremi  LV6 2021年1月8日
木头人  LV24 2018年1月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友