js操作map集合

  1. /**  
  2.  * js实现的map  
  3.  * Author:penghuaiyi  
  4.  * Date:2009-06-04  
  5. */  
  6.     
  7.  //定义map   
  8.  function Map()   
  9.  {    
  10.     this.container = {};    
  11.  }    
  12.   
  13.  //将key-value放入map中   
  14.  Map.prototype.put = function(key,value){    
  15.   try{    
  16.        
  17.     if(key!=null && key != "")   
  18.        this.container[key] = value;    
  19.   
  20.    }catch(e){    
  21.      return e;    
  22.     }    
  23.  };    
  24.   
  25.  //根据key从map中取出对应的value   
  26.  Map.prototype.get = function(key){    
  27.   try{    
  28.   
  29.      return this.container[key];    
  30.   
  31.   }catch(e){    
  32.      return e;    
  33.   }    
  34.  };    
  35.   
  36.  //判断map中是否包含指定的key   
  37.  Map.prototype.containsKey=function(key){   
  38.   
  39.   try{   
  40.       for(var p in this.container)   
  41.       {   
  42.          if(this.p==key)   
  43.             return true;   
  44.       }   
  45.   
  46.       return false;   
  47.     
  48.   }catch(e){   
  49.       return e;   
  50.   }   
  51.   
  52. }   
  53.   
  54.  //判断map中是否包含指定的value   
  55.  Map.prototype.containsValue = function(value){    
  56.   try{    
  57.   
  58.    for(var p in this.container)   
  59.    {    
  60.      if(this.container[p] === value)    
  61.         return true;    
  62.     }    
  63.   
  64.     return false;    
  65.   
  66.  }catch(e){    
  67.     return e;    
  68.  }    
  69.  };    
  70.   
  71.   
  72.  //删除map中指定的key   
  73.  Map.prototype.remove = function(key){    
  74.   try{    
  75.   
  76.      delete this.container[key];    
  77.     
  78.    }catch(e){    
  79.      return e;    
  80.    }    
  81.  };    
  82.   
  83.   //清空map   
  84.   Map.prototype.clear = function(){    
  85.    try{    
  86.      delete this.container;    
  87.      this.container = {};    
  88.   
  89.     }catch(e){    
  90.        return e;    
  91.     }    
  92.   };    
  93.   
  94.   //判断map是否为空   
  95.   Map.prototype.isEmpty = function(){    
  96.        
  97.     if(this.keyArray().length==0)   
  98.        return true;   
  99.     else    
  100.        return false;   
  101.   };    
  102.      
  103.   //获取map的大小   
  104.   Map.prototype.size=function(){   
  105.      
  106.      return this.keyArray().length;   
  107.   }   
  108.   
  109.   //返回map中的key值数组   
  110.   Map.prototype.keyArray=function(){   
  111.   
  112.        var keys=new Array();   
  113.        for(var p in this.container)   
  114.        {   
  115.            keys.push(p);   
  116.        }   
  117.        
  118.        return keys;   
  119.  }   
  120.   
  121.   //返回map中的value值数组   
  122.   Map.prototype.valueArray=function(){   
  123.       
  124.        var values=new Array();   
  125.        var keys=this.keyArray();   
  126.        for(var i=0;i<keys.length;i++)   
  127.        {   
  128.           values.push(this.container[keys[i]]);   
  129.        }   
  130.           
  131.        return values;   
  132.   }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值