http://blog.sina.com.cn/s/blog_7e9c5b6801016oyz.html
var map = new Map();
map.put("a", "aaa");
map.put("b","bbb");
map.put("cc","cccc");
map.put("c","ccc");
map.remove("cc");
var array = map.keySet();
for(var i in array) {
document.write("key:(" + array[i] +") <br>value: ("+map.get(array[i])+") <br>");
}
function Map(){
this.container = new Object();
}
Map.prototype.put = function(key, value){
this.container[key] = value;
}
Map.prototype.get = function(key){
return this.container[key];
}
Map.prototype.keySet = function() {
var keyset = new Array();
var count = 0;
for (var key in this.container) {
// 跳过object的extend函数
if (key == 'extend') {
continue;
}
keyset[count] = key;
count++;
}
return keyset;
}
Map.prototype.size = function() {
var count = 0;
for (var key in this.container) {
// 跳过object的extend函数
if (key == 'extend'){
continue;
}
count++;
}
return count;
}
Map.prototype.remove = function(key) {
delete this.container[key];
}
Map.prototype.toString = function(){
var str = "";
for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
str = str + keys[i] + "=" + this.container[keys[i]] + ";\n";
}
return str;
}
}
function Map(){
this.container = new Object();
}
Map.prototype.put = function(key, value){
this.container[key] = value;
}
Map.prototype.get = function(key){
return this.container[key];
}
Map.prototype.keySet = function() {
var keyset = new Array();
var count = 0;
for (var key in this.container) {
// 跳过object的extend函数
if (key == 'extend') {
continue;
}
keyset[count] = key;
count++;
}
return keyset;
}
Map.prototype.size = function() {
var count = 0;
for (var key in this.container) {
// 跳过object的extend函数
if (key == 'extend'){
continue;
}
count++;
}
return count;
}
Map.prototype.remove = function(key) {
delete this.container[key];
}
Map.prototype.toString = function(){
var str = "";
for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
str = str + keys[i] + "=" + this.container[keys[i]] + ";\n";
}
return str;
}
1 /* 2 * MAP对象,实现MAP功能 3 * 4 * 接口: 5 * size() 获取MAP元素个数 6 * isEmpty() 判断MAP是否为空 7 * clear() 删除MAP所有元素 8 * put(key, value) 向MAP中增加元素(key, value) 9 * remove(key) 删除指定KEY的元素,成功返回True,失败返回False 10 * get(key) 获取指定KEY的元素值VALUE,失败返回NULL 11 * element(index) 获取指定索引的元素(使用element.key,element.value获取KEY和VALUE),失败返回NULL 12 * containsKey(key) 判断MAP中是否含有指定KEY的元素 13 * containsValue(value) 判断MAP中是否含有指定VALUE的元素 14 * values() 获取MAP中所有VALUE的数组(ARRAY) 15 * keys() 获取MAP中所有KEY的数组(ARRAY) 16 * 17 * 例子: 18 * var map = new Map(); 19 * 20 * map.put("key", "value"); 21 * var val = map.get("key") 22 * …… 23 * 24 */ 25 function Map() { 26 this.elements = new Array(); 27 28 //获取MAP元素个数 29 this.size = function() { 30 return this.elements.length; 31 }; 32 33 //判断MAP是否为空 34 this.isEmpty = function() { 35 return (this.elements.length < 1); 36 }; 37 38 //删除MAP所有元素 39 this.clear = function() { 40 this.elements = new Array(); 41 }; 42 43 //向MAP中增加元素(key, value) 44 this.put = function(_key, _value) { 45 this.elements.push( { 46 key : _key, 47 value : _value 48 }); 49 }; 50 51 //删除指定KEY的元素,成功返回True,失败返回False 52 this.removeByKey = function(_key) { 53 var bln = false; 54 try { 55 for (i = 0; i < this.elements.length; i++) { 56 if (this.elements[i].key == _key) { 57 this.elements.splice(i, 1); 58 return true; 59 } 60 } 61 } catch (e) { 62 bln = false; 63 } 64 return bln; 65 }; 66 67 //删除指定VALUE的元素,成功返回True,失败返回False 68 this.removeByValue = function(_value) {//removeByValueAndKey 69 var bln = false; 70 try { 71 for (i = 0; i < this.elements.length; i++) { 72 if (this.elements[i].value == _value) { 73 this.elements.splice(i, 1); 74 return true; 75 } 76 } 77 } catch (e) { 78 bln = false; 79 } 80 return bln; 81 }; 82 83 //删除指定VALUE的元素,成功返回True,失败返回False 84 this.removeByValueAndKey = function(_key,_value) { 85 var bln = false; 86 try { 87 for (i = 0; i < this.elements.length; i++) { 88 if (this.elements[i].value == _value && this.elements[i].key == _key) { 89 this.elements.splice(i, 1); 90 return true; 91 } 92 } 93 } catch (e) { 94 bln = false; 95 } 96 return bln; 97 }; 98 99 //获取指定KEY的元素值VALUE,失败返回NULL 100 this.get = function(_key) { 101 try { 102 for (i = 0; i < this.elements.length; i++) { 103 if (this.elements[i].key == _key) { 104 return this.elements[i].value; 105 } 106 } 107 } catch (e) { 108 return false; 109 } 110 return false; 111 }; 112 113 //获取指定索引的元素(使用element.key,element.value获取KEY和VALUE),失败返回NULL 114 this.element = function(_index) { 115 if (_index < 0 || _index >= this.elements.length) { 116 return null; 117 } 118 return this.elements[_index]; 119 }; 120 121 //判断MAP中是否含有指定KEY的元素 122 this.containsKey = function(_key) { 123 var bln = false; 124 try { 125 for (i = 0; i < this.elements.length; i++) { 126 if (this.elements[i].key == _key) { 127 bln = true; 128 } 129 } 130 } catch (e) { 131 bln = false; 132 } 133 return bln; 134 }; 135 136 //判断MAP中是否含有指定VALUE的元素 137 this.containsValue = function(_value) { 138 var bln = false; 139 try { 140 for (i = 0; i < this.elements.length; i++) { 141 if (this.elements[i].value == _value) { 142 bln = true; 143 } 144 } 145 } catch (e) { 146 bln = false; 147 } 148 return bln; 149 }; 150 151 //判断MAP中是否含有指定VALUE的元素 152 this.containsObj = function(_key,_value) { 153 var bln = false; 154 try { 155 for (i = 0; i < this.elements.length; i++) { 156 if (this.elements[i].value == _value && this.elements[i].key == _key) { 157 bln = true; 158 } 159 } 160 } catch (e) { 161 bln = false; 162 } 163 return bln; 164 }; 165 166 //获取MAP中所有VALUE的数组(ARRAY) 167 this.values = function() { 168 var arr = new Array(); 169 for (i = 0; i < this.elements.length; i++) { 170 arr.push(this.elements[i].value); 171 } 172 return arr; 173 }; 174 175 //获取MAP中所有VALUE的数组(ARRAY) 176 this.valuesByKey = function(_key) { 177 var arr = new Array(); 178 for (i = 0; i < this.elements.length; i++) { 179 if (this.elements[i].key == _key) { 180 arr.push(this.elements[i].value); 181 } 182 } 183 return arr; 184 }; 185 186 //获取MAP中所有KEY的数组(ARRAY) 187 this.keys = function() { 188 var arr = new Array(); 189 for (i = 0; i < this.elements.length; i++) { 190 arr.push(this.elements[i].key); 191 } 192 return arr; 193 }; 194 195 //获取key通过value 196 this.keysByValue = function(_value) { 197 var arr = new Array(); 198 for (i = 0; i < this.elements.length; i++) { 199 if(_value == this.elements[i].value){ 200 arr.push(this.elements[i].key); 201 } 202 } 203 return arr; 204 }; 205 206 //获取MAP中所有KEY的数组(ARRAY) 207 this.keysRemoveDuplicate = function() { 208 var arr = new Array(); 209 for (i = 0; i < this.elements.length; i++) { 210 var flag = true; 211 for(var j=0;j<arr.length;j++){ 212 if(arr[j] == this.elements[i].key){ 213 flag = false; 214 break; 215 } 216 } 217 if(flag){ 218 arr.push(this.elements[i].key); 219 } 220 } 221 return arr; 222 }; 223 }