DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript中實現Map的示例代碼
JavaScript中實現Map的示例代碼
編輯:關於JavaScript     

不廢話了,直接貼代碼了。

代碼一:

var map=new Map();
map.put("a","A");map.put("b","B");map.put("c","C");
map.get("a"); //返回:A
map.entrySet() // 返回Entity[{key,value},{key,value}]
map.containsKey('kevin') //返回:false
function Map() { 
  this.keys = new Array(); 
  this.data = new Object(); 
  /** 
   * 放入一個鍵值對 
   * @param {String} key 
   * @param {Object} value 
   */ 
  this.put = function(key, value) { 
    if(this.data[key] == null){ 
      this.keys.push(key); 
      this.data[key] = value; 
    }else{ 
      this.data[key]=this.data[key]; 
    } 
    return true; 
  }; 
  /** 
   * 獲取某鍵對應的值 
   * @param {String} key 
   * @return {Object} value 
   */ 
  this.get = function(key) { 
    return this.data[key]; 
  }; 
  /** 
   * 刪除一個鍵值對 
   * @param {String} key 
   */ 
  this.remove = function(key) { 
    for(var i=0;i<this.keys.length;i++){ 
      if(key===this.keys[i]){ 
        var del_keys= this.keys.splice(i,1); 
        for(k in del_keys){ 
          this.data[k] = null; 
        } 
        return true; 
      } 
    } 
    return false; 
  }; 
  /** 
   * 遍歷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); 
    } 
  }; 
  /** 
   * 獲取鍵值數組 
   * @return entity[{key,value},{key,value}] 
   */ 
  this.entrySet = 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; 
  }; 
  /** 
   * 判斷Map是否為空 
   */ 
  this.isEmpty = function() { 
    return this.keys.length == 0; 
  }; 
  /** 
   * 獲取鍵值對數量 
   */ 
  this.size = function(){ 
    return this.keys.length; 
  }; 
  this.containsKey=function(key){ 
    return this.keys.filter(function(v){ 
      if(v===key){ 
        return key; 
      } 
    }).length>0; 
  }; 
  /** 
   * 重寫toString 
   */ 
  this.toString = function(){ 
    var s = "{"; 
    for(var i=0;i<this.keys.length;i++){ 
      var k = this.keys[i]; 
      s += k+"="+this.data[k]; 
      if(this.keys.length>i+1){ 
        s+=',' 
      } 
    } 
    s+="}"; 
    return s; 
  }; 
  /** 
   * 解析字符串到Map 
   * {a=A,b=B,c=B,} 
   */ 
  this.parserStringAndAddMap=function(str){ 
    var count=0; 
    if(str && str.length>0){ 
      str=str.trim(); 
      var startIndex=str.indexOf("{"),endIndex=str.lastIndexOf("}"); 
      if(startIndex!==-1 && endIndex!==-1){ 
        str=str.substring(startIndex+1,endIndex); 
        var arrs= str.split(","); 
        for(var i=0;i<arrs.length;i++){ 
          var kv=arrs[i].trim(); 
          if(kv.length>0 && kv.indexOf("=")!==-1){ 
            var kv_arr=kv.split("="); 
            if(kv_arr.length==2){ 
              if(this.put(kv_arr[0].trim(),kv_arr[1].trim())){ 
                count++; 
              }else{ 
                console.error('error: kv:'+kv); 
              } 
            } 
          } 
        } 
      }else{ 
        console.log("data error:"+str); 
      } 
    }else{ 
      console.log('data is not empty'); 
    } 
    return count; 
  }; 
} 

代碼二:

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() {
  /** 存放鍵的數組(遍歷用到) */
  this.keys = new Array();
  /** 存放數據 */
  this.data = new Object();
  /**
   * 放入一個鍵值對
   * @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];
  };
  /**
   * 刪除一個鍵值對
   * @param {String} key
   */
  this.remove = function(key) {
    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;
  };
}
function testMap(){
  var m = new Map();
  m.put('key1','Comtop');
  m.put('key2','南方電網');
  m.put('key3','景新花園');
  alert("init:"+m);
  m.put('key1','康拓普');
  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);
}

以上內容通過兩段代碼給大家分享了JavaScript中實現Map,希望大家喜歡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved