js实现的Map、List,-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

js实现的Map、List,

a123456678 2016-07-15 11:51:04 1820
/**
  * js实现list
  *
  */
function List(){
 
    this.value = [];
     
 
    /*添加*/
    this.add = function(obj){
        return this.value.push(obj);
    };
     
    /*大小*/
    this.size = function(){
        return this.value.length;
    }
 
    /*返回指定索引的值*/
    this.get = function(index){
        return this.value[index];
    }
 
    /*删除指定索引的值*/
    this.remove = function(index){
        this.value1 = [];                           
        this.value[index]='';                       
        for(var i=0;i<this.size();i++){
            if(this.value[i]!=''){                  
                this.value1.push(this.value[i]);
            }
        }
        this.value = this.value1;   
        delete this.value1;         
        return this.value;          
    }
     
    /*删除全部值*/
    this.removeAll = function(){
        return this.value=[];           
    }
 
    /*是否包含某个对象*/
    this.constains = function(obj){
        for(var i in this.value){       
            if( obj == this.value[i] ){ 
                return true;
            }else{                      
                continue;
            }
        }
        return false;               
    }
 
}
Map

/**
 * js Map 实现 
 * @returns {Map}
 */
function Map() {
    this.keys = [];
    this.data = {};
 
    /**
     * 放入一个键值对
     * @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];
    };
 
    /**
     * 是否包含该键
     */
    this.contain = function(key) {
        
        var value = this.data[key];
        if (value)
            return true;
        else
            return false;
    };
 
    /**
     * 删除一个键值对
     * @param {String} key
     */
    this.remove = function(key) {
        for(var index=0;index<this.keys.length;index++){
            if(this.keys[index]==key){
                this.keys.splice(index,1);
                break;
            }
        }
        //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;
    };
}
mapjs实现
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:56:52
    this.remove = function(index){
        this.value.splice(index,1);
        return this.value;
    }
    0 0
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

相似问题
最新问题