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