A simple JavaScript Map instance of sharing

  • 2021-07-07 06:28:05
  • OfStack

I wrote an Map with js, with traversal function. Please comment on it.

//map.js


Array.prototype.remove = function(s) { 
  for (var i = 0; i < this.length; i++) { 
    if (s == this[i]) 
      this.splice(i, 1); 
  } 
} 
 
/** 
 * Simple Map 
 * 
 * 
 * var m = new Map(); 
 * m.put('key','value'); 
 * ... 
 * var s = ""; 
 * m.each(function(key,value,index){ 
 *   s += index+":"+ key+"="+value+"\n"; 
 * }); 
 * alert(s); 
 * 
 * @author dewitt 
 * @date 2008-05-24 
 */
function Map() { 
  /**  Array for storing keys ( Traversal uses ) */
  this.keys = new Array(); 
  /**  Store data  */
  this.data = new Object(); 
   
  /** 
   *  Put in 1 Key-value pairs  
   * @param {String} key 
   * @param {Object} value 
   */
  this.put = function(key, value) { 
    if(this.data[key] == null){ 
      this.keys.push(key); 
    } 
    this.data[key] = value; 
  }; 
   
  /** 
   *  Gets the value corresponding to a key  
   * @param {String} key 
   * @return {Object} value 
   */
  this.get = function(key) { 
    return this.data[key]; 
  }; 
   
  /** 
   *  Delete 1 Key-value pairs  
   * @param {String} key 
   */
  this.remove = function(key) { 
    this.keys.remove(key); 
    this.data[key] = null; 
  }; 
   
  /** 
   *  Traversal Map, Execute handler function  
   * 
   * @param {Function}  Callback 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); 
    } 
  }; 
   
  /** 
   *  Get key-value array ( Similar Java Adj. entrySet()) 
   * @return  Key value object {key,value} Array of  
   */
  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; 
  }; 
   
  /** 
   *  Judge Map Whether it is empty or not  
   */
  this.isEmpty = function() { 
    return this.keys.length == 0; 
  }; 
   
  /** 
   *  Get the number of key-value logarithms  
   */
  this.size = function(){ 
    return this.keys.length; 
  }; 
   
  /** 
   *  Rewrite 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',' Southern power grid '); 
  m.put('key3',' Jingxin Garden '); 
  alert("init:"+m); 
   
  m.put('key1',' Kangtopu '); 
  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); 
} 
    //testMap.htm

?
<html> 
<head> 
<title>Test Map</title> 
<script language="javascript" src="map.js"> 
</script> 
</head> 
<body> 
<input type="button" value="test" onclick="testMap()"> 
</body> 
</html> 

Related articles: