欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Javacript中自定义的map.js 的方法

程序员文章站 2022-04-09 20:53:22
 js中没有map这个类,只能自己写一个。以下map.js和map-util.js都是自定义的map,任选其一就可以。你可以用它来像java里new map()和...

 js中没有map这个类,只能自己写一个。以下map.js和map-util.js都是自定义的map,任选其一就可以。你可以用它来像java里new map()和put()、remove()、get()等方法。

map.js:

function map() {   
  var struct = function(key, value) {   
    this.key = key;   
    this.value = value;   
  }   
  var put = function(key, value){   
    for (var i = 0; i < this.arr.length; i++) {   
      if ( this.arr[i].key === key ) {   
        this.arr[i].value = value;   
        return;   
      }   
    }   
    this.arr[this.arr.length] = new struct(key, value);   
  }   
  var get = function(key) {   
    for (var i = 0; i < this.arr.length; i++) {   
      if ( this.arr[i].key === key ) {   
        return this.arr[i].value;   
      }   
    }   
    return null;   
  }   
  var remove = function(key) {   
    var v;   
    for (var i = 0; i < this.arr.length; i++) {   
      v = this.arr.pop();   
      if ( v.key === key ) {   
        continue;   
      }   
      this.arr.unshift(v);   
    }   
  }   
  var size = function() {   
    return this.arr.length;   
  }   
  var isempty = function() {   
    return this.arr.length <= 0;   
  }   
  this.arr = new array();   
  this.get = get;   
  this.put = put;   
  this.remove = remove;   
  this.size = size;   
  this.isempty = isempty;   
}   

map-util.js:

function map() { 
  this.elements = new array(); 
  var i; 
  //获取map元素个数 
  this.size = function() { 
    return this.elements.length; 
  }; 
  //判断map是否为空 
  this.isempty = function() { 
    return (this.elements.length < 1); 
  }; 
  //删除map所有元素 
  this.clear = function() { 
    this.elements = new array(); 
  }; 
  //向map中增加元素(key, value)  
  this.put = function(_key, _value) { 
    this.elements.push( { 
      key : _key, 
      value : _value 
    }); 
  }; 
  this.putfirst = function(_key, _value){ 
    var templist = this.elements; 
    this.elements = new array(); 
    this.elements.push( { 
      key : _key, 
      value : _value 
    }); 
    for(var i=0;i<templist.length;i++){ 
      this.elements.push( 
        templist[i] 
      ); 
    } 
  } 
  //删除指定key的元素,成功返回true,失败返回false 
  this.remove = function(_key) { 
    var bln = false; 
    try { 
      for (i = 0; i < this.elements.length; i++) { 
        if (this.elements[i].key == _key) { 
          this.elements.splice(i, 1); 
          return true; 
        } 
      } 
    } catch (e) { 
      bln = false; 
    } 
    return bln; 
  }; 
  //获取指定key的元素值value,失败返回null 
  this.get = function(_key) { 
    try { 
      for (i = 0; i < this.elements.length; i++) { 
        if (this.elements[i].key == _key) { 
          return this.elements[i].value; 
        } 
      } 
    } catch (e) { 
      return null; 
    } 
  }; 
  //获取指定索引的元素(使用element.key,element.value获取key和value),失败返回null 
  this.element = function(_index) { 
    if (_index < 0 || _index >= this.elements.length) { 
      return null; 
    } 
    return this.elements[_index]; 
  }; 
  //判断map中是否含有指定key的元素 
  this.containskey = function(_key) { 
    var bln = false; 
    try { 
      for (i = 0; i < this.elements.length; i++) { 
        if (this.elements[i].key == _key) { 
          bln = true; 
        } 
      } 
    } catch (e) { 
      bln = false; 
    } 
    return bln; 
  }; 
  //判断map中是否含有指定value的元素 
  this.containsvalue = function(_value) { 
    var bln = false; 
    try { 
      for (i = 0; i < this.elements.length; i++) { 
        if (this.elements[i].value == _value) { 
          bln = true; 
        } 
      } 
    } catch (e) { 
      bln = false; 
    } 
    return bln; 
  }; 
  //获取map中所有value的数组(array) 
  this.values = function() { 
    var arr = new array(); 
    for (i = 0; i < this.elements.length; i++) { 
      arr.push(this.elements[i].value); 
    } 
    return arr; 
  }; 
  //获取map中所有key的数组(array) 
  this.keys = function() { 
    var arr = new array(); 
    for (i = 0; i < this.elements.length; i++) { 
      arr.push(this.elements[i].key); 
    } 
    return arr; 
  }; 
} 

总结

以上所述是小编给大家介绍的 javacript中自定义的map.js  的方法,希望对大家有所帮助