DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript中利用Array和Object實現Map的方法
JavaScript中利用Array和Object實現Map的方法
編輯:關於JavaScript     

本文實例講述了JavaScript中利用Array和Object實現Map的方法。分享給大家供大家參考。具體如下:

昨天突然看到以前別人用JavaScript實現的Map感覺很不錯,但是發現有個別方法有問題,順便完善了下,添加了 remove 、indexOf 、values、clear等方法。

/**
 * @author blune68
 * @version 0.1, 07/27/12
 * 
 */
function Map(){
  this.keys = new Array(); 
  this.data = new Object();
  var toString = Object.prototype.toString;
  /**
   * 當前Map當前長度
   */
  this.size = function(){
    return this.keys.length;
  }
  /**
   * 添加值
   * @param {Object} key
   * @param {Object} value
   */
  this.put = function(key, value){
    if(this.data[key] == null){
      this.data[key] = value;
    }
    this.keys.push(key);
  }
  /**
   * 根據當前key獲取value
   * @param {Object} key
   */
  this.get = function(key){
    return this.data[key];
  }
  /**
   * 根據當前key移除Map對應值
   * @param {Object} key
   */
  this.remove = function(key){
    var index = this.indexOf(key);
    if(index != -1){
      this.keys.splice(index, 1);
    }
    this.data[key] = null;
  }
  /**
   * 清空Map
   */
  this.clear = function(){
    for(var i=0, len = this.size(); i < len; i++){
      var key = this.keys[i];
      this.data[key] = null;
    }
    this.keys.length = 0;
  }
  /**
   * 當前key是否存在
   * @param {Object} key
   */
  this.containsKey = function(key){
    return this.data[key] != null;
  }
  /**
   * 是否為空
   */
  this.isEmpty = function(){
    return this.keys.length === 0;
  }
  /**
   * 類型Java中Map.entrySet
   */
  this.entrySet = function(){
    var size = this.size();
    var datas = new Array(size);
    for (var i = 0, len = size; i < len; i++) {
      var key = this.keys[i];
      var value = this.data[key];
      datas[i] = {
        'key' : key,
        'value':value  
      }
    }
    return datas;
  }
  /**
   * 遍歷當前Map
   * var map = new Map();
   * map.put('key', 'value');
   * map.each(function(index, key, value){
   *   console.log("index:" + index + "--key:" + key + "--value:" + value)
   * })
   * @param {Object} fn
   */
  this.each = function(fn){
    if(toString.call(fn) === '[object Function]'){
      for (var i = 0, len = this.size(); i < len; i++) {
        var key = this.keys[i];
        fn(i, key, this.data[key]);
      }
    }
    return null;
  }
  /**
   * 獲取Map中 當前key 索引值
   * @param {Object} key
   */
  this.indexOf = function(key){
    var size = this.size();
    if(size > 0){
      for(var i=0, len=size; i < len; i++){
        if(this.keys[i] == key)
        return i;
      }
    }
    return -1;
  }
  /**
   * Override toString
   */
  this.toString = function(){
    var str = "{";
    for (var i = 0, len = this.size(); i < len; i++, str+=",") {
      var key = this.keys[i];
      var value = this.data[key];
      str += key + "=" + value; 
    }
    str = str.substring(0, str.length-1);
    str += "}";
    return str;
  }
  /**
   * 獲取Map中的所有value值(Array)
   */
  this.values = function(){
    var size = this.size();
    var values = new Array();
    for(var i = 0; i < size; i++){
      var key = this.keys[i];
      values.push(this.data[key]);
    }
    return values;
  }
}

希望本文所述對大家的javascript程序設計有所幫助。

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