首页 > 语言 > JavaScript > 正文

JavaScript中利用Array和Object实现Map的方法

2024-05-06 16:23:49
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了JavaScript中利用Array和Object实现Map的方法,实例分析了javascript实现map的添加、获取、移除、清空、遍历等操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了JavaScript中利用Array和Object实现Map的方法。分享给大家供大家参考。具体如下:

昨天突然看到以前别人用JavaScript实现的Map感觉很不错,但是发现有个别方法有问题,顺便完善了下,添加了 remove 、indexOf 、values、clear等方法。

 

 
  1. /** 
  2. * @author blune68 
  3. * @version 0.1, 07/27/12 
  4. *  
  5. */ 
  6. function Map(){ 
  7. this.keys = new Array();  
  8. this.data = new Object(); 
  9. var toString = Object.prototype.toString; 
  10. /** 
  11. * 当前Map当前长度 
  12. */ 
  13. this.size = function(){ 
  14. return this.keys.length; 
  15. } 
  16. /** 
  17. * 添加值 
  18. * @param {Object} key 
  19. * @param {Object} value 
  20. */ 
  21. this.put = function(key, value){ 
  22. if(this.data[key] == null){ 
  23. this.data[key] = value; 
  24. } 
  25. this.keys.push(key); 
  26. } 
  27. /** 
  28. * 根据当前key获取value 
  29. * @param {Object} key 
  30. */ 
  31. this.get = function(key){ 
  32. return this.data[key]; 
  33. } 
  34. /** 
  35. * 根据当前key移除Map对应值 
  36. * @param {Object} key 
  37. */ 
  38. this.remove = function(key){ 
  39. var index = this.indexOf(key); 
  40. if(index != -1){ 
  41. this.keys.splice(index, 1); 
  42. } 
  43. this.data[key] = null; 
  44. } 
  45. /** 
  46. * 清空Map 
  47. */ 
  48. this.clear = function(){ 
  49. for(var i=0, len = this.size(); i < len; i++){ 
  50. var key = this.keys[i]; 
  51. this.data[key] = null; 
  52. } 
  53. this.keys.length = 0; 
  54. } 
  55. /** 
  56. * 当前key是否存在 
  57. * @param {Object} key 
  58. */ 
  59. this.containsKey = function(key){ 
  60. return this.data[key] != null; 
  61. } 
  62. /** 
  63. * 是否为空 
  64. */ 
  65. this.isEmpty = function(){ 
  66. return this.keys.length === 0; 
  67. } 
  68. /** 
  69. * 类型Java中Map.entrySet 
  70. */ 
  71. this.entrySet = function(){ 
  72. var size = this.size(); 
  73. var datas = new Array(size); 
  74. for (var i = 0, len = size; i < len; i++) { 
  75. var key = this.keys[i]; 
  76. var value = this.data[key]; 
  77. datas[i] = { 
  78. 'key' : key, 
  79. 'value':value  
  80. } 
  81. } 
  82. return datas; 
  83. } 
  84. /** 
  85. * 遍历当前Map 
  86. * var map = new Map(); 
  87. * map.put('key', 'value'); 
  88. * map.each(function(index, key, value){ 
  89. * console.log("index:" + index + "--key:" + key + "--value:" + value) 
  90. * }) 
  91. * @param {Object} fn 
  92. */ 
  93. this.each = function(fn){ 
  94. if(toString.call(fn) === '[object Function]'){ 
  95. for (var i = 0, len = this.size(); i < len; i++) { 
  96. var key = this.keys[i]; 
  97. fn(i, key, this.data[key]); 
  98. } 
  99. } 
  100. return null; 
  101. } 
  102. /** 
  103. * 获取Map中 当前key 索引值 
  104. * @param {Object} key 
  105. */ 
  106. this.indexOf = function(key){ 
  107. var size = this.size(); 
  108. if(size > 0){ 
  109. for(var i=0, len=size; i < len; i++){ 
  110. if(this.keys[i] == key) 
  111. return i; 
  112. } 
  113. } 
  114. return -1; 
  115. } 
  116. /** 
  117. * Override toString 
  118. */ 
  119. this.toString = function(){ 
  120. var str = "{"; 
  121. for (var i = 0, len = this.size(); i < len; i++, str+=",") { 
  122. var key = this.keys[i]; 
  123. var value = this.data[key]; 
  124. str += key + "=" + value;  
  125. } 
  126. str = str.substring(0, str.length-1); 
  127. str += "}"; 
  128. return str; 
  129. } 
  130. /** 
  131. * 获取Map中的所有value值(Array) 
  132. */ 
  133. this.values = function(){ 
  134. var size = this.size(); 
  135. var values = new Array(); 
  136. for(var i = 0; i < size; i++){ 
  137. var key = this.keys[i]; 
  138. values.push(this.data[key]); 
  139. } 
  140. return values; 
  141. } 
  142. } 

希望本文所述对大家的javascript程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选