MAP IN JS
- source page :?? http://www.iteye.com/topic/196610
- Array.prototype.remove?=? function (s)?{ ??
- ???? for ?( var ?i?=?0;?i?<? this .length;?i++)?{ ??
- ???????? if ?(s?==? this [i]) ??
- ???????????? this .splice(i,?1); ??
- ????} ??
- } ??
- ??
- /** ?
- ?*?Simple?Map ?
- ?*? ?
- ?*? ?
- ?*?var?m?=?new?Map(); ?
- ?*?m.put('key','value'); ?
- ?*?... ?
- ?*?var?s?=?""; ?
- ?*?m.each(function(key,value,index){ ?
- ?*??????s?+=?index+":"+?key+"="+value+"\n"; ?
- ?*?}); ?
- ?*?alert(s); ?
- ?*? ?
- ?*?@author?dewitt ?
- ?*?@date?2008-05-24 ?
- ?*/ ??
- function ?Map()?{ ??
- ???? /**?存放鍵的數(shù)組(遍歷用到)?*/ ??
- ???? this .keys?=? new ?Array(); ??
- ???? /**?存放數(shù)據(jù)?*/ ??
- ???? this .data?=? new ?Object(); ??
- ???? ??
- ???? /** ?
- ?????*?放入一個(gè)鍵值對(duì) ?
- ?????*?@param?{String}?key ?
- ?????*?@param?{Object}?value ?
- ?????*/ ??
- ???? this .put?=? function (key,?value)?{ ??
- ???????? if ( this .data[key]?==? null ){ ??
- ???????????? this .keys.push(key); ??
- ????????} ??
- ???????? this .data[key]?=?value; ??
- ????}; ??
- ???? ??
- ???? /** ?
- ?????*?獲取某鍵對(duì)應(yīng)的值 ?
- ?????*?@param?{String}?key ?
- ?????*?@return?{Object}?value ?
- ?????*/ ??
- ???? this .get?=? function (key)?{ ??
- ???????? return ? this .data[key]; ??
- ????}; ??
- ???? ??
- ???? /** ?
- ?????*?刪除一個(gè)鍵值對(duì) ?
- ?????*?@param?{String}?key ?
- ?????*/ ??
- ???? this .remove?=? function (key)?{ ??
- ???????? this .keys.remove(key); ??
- ???????? this .data[key]?=? null ; ??
- ????}; ??
- ???? ??
- ???? /** ?
- ?????*?遍歷Map,執(zhí)行處理函數(shù) ?
- ?????*? ?
- ?????*?@param?{Function}?回調(diào)函數(shù)?function(key,value,index){..} ?
- ?????*/ ??
- ???? this .each?=? function (fn){ ??
- ???????? if ( typeof ?fn?!=? 'function' ){ ??
- ???????????? return ; ??
- ????????} ??
- ???????? var ?len?=? this .keys.length; ??
- ???????? for ( var ?i=0;i<len;i++){ ??
- ???????????? var ?k?=? this .keys[i]; ??
- ????????????fn(k, this .data[k],i); ??
- ????????} ??
- ????}; ??
- ???? ??
- ???? /** ?
- ?????*?獲取鍵值數(shù)組(類似Java的entrySet()) ?
- ?????*?@return?鍵值對(duì)象{key,value}的數(shù)組 ?
- ?????*/ ??
- ???? this .entrys?=? function ()?{ ??
- ???????? var ?len?=? this .keys.length; ??
- ???????? var ?entrys?=? new ?Array(len); ??
- ???????? for ?( var ?i?=?0;?i?<?len;?i++)?{ ??
- ????????????entrys[i]?=?{ ??
- ????????????????key?:? this .keys[i], ??
- ????????????????value?:? this .data[i] ??
- ????????????}; ??
- ????????} ??
- ???????? return ?entrys; ??
- ????}; ??
- ???? ??
- ???? /** ?
- ?????*?判斷Map是否為空 ?
- ?????*/ ??
- ???? this .isEmpty?=? function ()?{ ??
- ???????? return ? this .keys.length?==?0; ??
- ????}; ??
- ???? ??
- ???? /** ?
- ?????*?獲取鍵值對(duì)數(shù)量 ?
- ?????*/ ??
- ???? this .size?=? function (){ ??
- ???????? return ? this .keys.length; ??
- ????}; ??
- ???? ??
- ???? /** ?
- ?????*?重寫toString? ?
- ?????*/ ??
- ???? this .toString?=? function (){ ??
- ???????? var ?s?=? "{" ; ??
- ???????? for ( var ?i=0;i< this .keys.length;i++,s+= ',' ){ ??
- ???????????? var ?k?=? this .keys[i]; ??
- ????????????s?+=?k+ "=" + this .data[k]; ??
- ????????} ??
- ????????s+= "}" ; ??
- ???????? return ?s; ??
- ????}; ??
- } ??
- ??
- ??
- function ?testMap(){ ??
- ???? var ?m?=? new ?Map(); ??
- ????m.put( 'key1' , 'Comtop' ); ??
- ????m.put( 'key2' , '南方電網(wǎng)' ); ??
- ????m.put( 'key3' , '景新花園' ); ??
- ????alert( "init:" +m); ??
- ???? ??
- ????m.put( 'key1' , '康拓普' ); ??
- ????alert( "set?key1:" +m); ??
- ???? ??
- ????m.remove( "key2" ); ??
- ????alert( "remove?key2:?" +m); ??
- ???? ??
- ???? var ?s?= "" ; ??
- ????m.each( function (key,value,index){ ??
- ????????s?+=?index+ ":" +?key+ "=" +value+ "\n" ; ??
- ????}); ??
- ????alert(s); ??
- }??
Array.prototype.remove = function(s) { for (var i = 0; i < this.length; i++) { if (s == this[i]) this.splice(i, 1); } } /** * Simple Map * * * var m = new Map(); * m.put('key','value'); * ... * var s = ""; * m.each(function(key,value,index){ * s += index+":"+ key+"="+value+"\n"; * }); * alert(s); * * @author dewitt * @date 2008-05-24 */ function Map() { /** 存放鍵的數(shù)組(遍歷用到) */ this.keys = new Array(); /** 存放數(shù)據(jù) */ this.data = new Object(); /** * 放入一個(gè)鍵值對(duì) * @param {String} key * @param {Object} value */ this.put = function(key, value) { if(this.data[key] == null){ this.keys.push(key); } this.data[key] = value; }; /** * 獲取某鍵對(duì)應(yīng)的值 * @param {String} key * @return {Object} value */ this.get = function(key) { return this.data[key]; }; /** * 刪除一個(gè)鍵值對(duì) * @param {String} key */ this.remove = function(key) { this.keys.remove(key); this.data[key] = null; }; /** * 遍歷Map,執(zhí)行處理函數(shù) * * @param {Function} 回調(diào)函數(shù) function(key,value,index){..} */ this.each = function(fn){ if(typeof fn != 'function'){ return; } var len = this.keys.length; for(var i=0;i<len;i++){ var k = this.keys[i]; fn(k,this.data[k],i); } }; /** * 獲取鍵值數(shù)組(類似Java的entrySet()) * @return 鍵值對(duì)象{key,value}的數(shù)組 */ this.entrys = function() { var len = this.keys.length; var entrys = new Array(len); for (var i = 0; i < len; i++) { entrys[i] = { key : this.keys[i], value : this.data[i] }; } return entrys; }; /** * 判斷Map是否為空 */ this.isEmpty = function() { return this.keys.length == 0; }; /** * 獲取鍵值對(duì)數(shù)量 */ this.size = function(){ return this.keys.length; }; /** * 重寫toString */ this.toString = function(){ var s = "{"; for(var i=0;i<this.keys.length;i++,s+=','){ var k = this.keys[i]; s += k+"="+this.data[k]; } s+="}"; return s; }; } function testMap(){ var m = new Map(); m.put('key1','Comtop'); m.put('key2','南方電網(wǎng)'); m.put('key3','景新花園'); alert("init:"+m); m.put('key1','康拓普'); alert("set key1:"+m); m.remove("key2"); alert("remove key2: "+m); var s =""; m.each(function(key,value,index){ s += index+":"+ key+"="+value+"\n"; }); alert(s); }
?//testMap.htm
- < html > ??
- < head > ??
- < title > Test?Map </ title > ??
- < script ? language = "javascript" ? src = "map.js" > ??
- </ script > ??
- </ head > ??
- < body > ??
- < input ? type = "button" ? value = "test" ? onclick = "testMap()" > ??
- </ body > ??
-
</
html
>
?
posted on 2011-08-05 17:16 picture talk 閱讀(222) 評(píng)論(0) 編輯 收藏