JavaScript Map数据结构
1 Array.prototype.remove = function (s) { 2 for (var i = 0; i < this.length; i++) { 3 if (s == this[i]) 4 this.splice(i, 1); 5 } 6 } 7 8 /** 9 * Simple Map 10 * 11 * 12 * var m = new Map(); m.put('key','value'); ... var s = ""; 13 * m.each(function(key,value,index){ s += index+":"+ key+"="+value+"\n"; }); 14 * alert(s); 15 * 16 * @author dewitt 17 * @date 2008-05-24 18 */ 19 function Map() { 20 /** 存放键的数组(遍历用到) */ 21 this.keys = new Array(); 22 /** 存放数据 */ 23 this.data = new Object(); 24 25 this.maxSize = arguments[0] ? arguments[0] : 0; 26 27 /** 28 * 放入一个键值对 29 * 30 * @param {String} 31 * key 32 * @param {Object} 33 * value 34 */ 35 this.put = function (key, value) { 36 if (this.data[key] == null) { 37 this.keys.push(key); 38 } 39 this.data[key] = value; 40 }; 41 42 /** 43 * 放入一个键值对, FIFO容量移除策略 44 * 45 * @param {String} 46 * key 47 * @param {Object} 48 * value 49 */ 50 this.putQ = function (key, value) { 51 if (this.data[key] == null) { 52 this.keys.push(key); 53 } 54 this.data[key] = value; 55 this.trimToSize(this.maxSize); 56 }; 57 58 this.trimToSize = function (max_size) { 59 var tmpSize = this.size; 60 if (tmpSize > max_size) { 61 this.each(function (key, value, index) { 62 if (index < tmpSize - max_size) { 63 this.remove(key); 64 } 65 }); 66 } 67 } 68 69 /** 70 * 获取某键对应的值 71 * 72 * @param {String} 73 * key 74 * @return {Object} value 75 */ 76 this.get = function (key) { 77 return this.data[key]; 78 }; 79 80 /** 81 * 删除一个键值对 82 * 83 * @param {String} 84 * key 85 */ 86 this.remove = function (key) { 87 this.keys.remove(key); 88 this.data[key] = null; 89 }; 90 91 /** 92 * 遍历Map,执行处理函数 93 * 94 * @param {Function} 95 * 回调函数 function(key,value,index){..} 96 */ 97 this.each = function (fn) { 98 if (typeof fn != 'function') { 99 return; 100 } 101 var len = this.keys.length; 102 for (var i = 0; i < len; i++) { 103 var k = this.keys[i]; 104 fn(k, this.data[k], i); 105 } 106 }; 107 108 /** 109 * 获取键值数组(类似Java的entrySet()) 110 * 111 * @return 键值对象{key,value}的数组 112 */ 113 this.entrys = function () { 114 var len = this.keys.length; 115 var entrys = new Array(len); 116 for (var i = 0; i < len; i++) { 117 entrys[i] = { 118 key: this.keys[i], 119 value: this.data[i] 120 }; 121 } 122 return entrys; 123 }; 124 125 /** 126 * 判断Map是否为空 127 */ 128 this.isEmpty = function () { 129 return this.keys.length == 0; 130 }; 131 132 /** 133 * 获取键值对数量 134 */ 135 this.size = function () { 136 return this.keys.length; 137 }; 138 139 /** 140 * 重写toString 141 */ 142 this.toString = function () { 143 var s = "{"; 144 for (var i = 0; i < this.keys.length; i++, s += ',') { 145 var k = this.keys[i]; 146 s += k + "=" + JSON.stringify(this.data[k]); 147 } 148 s += "}"; 149 return s; 150 }; 151 } 152 153 function testMap() { 154 var m = new Map(); 155 m.put('key1', 'v1'); 156 m.put('key2', 'v2'); 157 m.put('key3', 'v3'); 158 m.put('key4', 'v4'); 159 alert("init:" + m); 160 161 m.put('key1', 'v5'); 162 alert("set key1:" + m); 163 164 m.remove("key2"); 165 alert("remove key2: " + m); 166 167 var s = ""; 168 m.each(function (key, value, index) { 169 if (index < 2) { 170 m.remove(key); 171 } 172 }); 173 174 m.each(function (key, value, index) { 175 s += index + ":" + key + "=" + value + "\n"; 176 }); 177 alert(s); 178 }
prototype是原型,通过原型调用方法,后续会总结一下。
整个map相当于一个json对象,map = {keys : arr,values : abc,} 就是这种形式。通过map来存取数据,但不能存储大数据。
这就是数据结构,太重要了。