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.}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值