`
yanghongxia9
  • 浏览: 112653 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

js的map结构实现

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

相关推荐

Global site tag (gtag.js) - Google Analytics