有序集合SortMap

140 阅读1分钟
function SortMap() {
      this.keys = new Array();
      this.data = new Map();

      this.propKey = '';
      this.propValueKey = '';
      //添加键值对
        this.init = function (initArray,propKey,propValueKey) {
            this.propKey = propKey
            this.propValueKey = propValueKey
            initArray.forEach((item,index) => { 
                this.set(item[propKey],item[propValueKey])
            });
           return this;
      };

      this.set = function (key, value) {
        if (this.data[key] == null) {//如键不存在则身【键】数组添加键名
          this.keys.push(key);
        }
        this.data[key] = value;//给键赋值
           return this;
      };
       //根据索引加入
       this.insertByIndex = function (obj,index) {
           let key = obj[this.propKey]
           let value = obj[this.propValueKey]
            //插入指定位置
           this.keys.splice(index,0,key);  
          this.data[key] = value;//给键赋值
        return this;
      };

        //根据索引删除
        this.removeByIndex = function (index) {
            let key = this.keys[index]; 
            this.keys.splice(index,1);
          this.data[key] = null; 
      };

      //获取键对应的值 
      this.get = function (key) {
        return this.data[key];
      };
      //去除键值,(去除键数据中的键名及对应的值)
      this.remove = function (key) {
          let index = this.keys.indexOf(key)
          this.keys.splice(index,1);
        this.data[key] = null;
      };
      //判断键值元素是否为空
      this.isEmpty = function () {
        return this.keys.length == 0;
      };
      //获取键值元素大小
      this.size = function () {
        return this.keys.length;
      };
      //遍历Map,执行处理函数. 回调函数 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);   
        }   
      };  
      //获取键值数组,返回键值对象{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[this.keys[i]]     
          };     
        }             
        return entrys;     
      }; 
      //重写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];     
        }
           if(s.length > 0){
              s = s.substring(0,s.length-1)
           }
        s+="}";     
        return s;     
      };   
    }

    function testMap(){   
        var m = new SortMap();   
        // m.set("key1","value1");   
        // m.set("key2","value2");   
        // m.set("key3","value3");   
        // console.log("init:"+m);   

        let arr = [ 
            {prop: 'craftFlowNum', label: '工序流', width: '46'},
            {prop: 'craftCode', label: '工序编码', width: '90'},
            {prop: 'craftPartName', label: '结构部件', width: '86'},
        ]
        m.init(arr,'prop','width') 
        m.insertByIndex( {prop: 'xxx', label: 'xxx', width: '100'},2) 
        console.log("init:"+m);   
        m.removeByIndex(0)
        console.log("init:"+m);   
      
    //    var s ="";   
    //     m.each(function(key,value,index){   
    //         s += index+":"+ key+"="+value+"/n";   
    //     });   
    //     console.log(s);   
    }
    testMap()