No nonsense, directly affixed to the code.
Code One:
var map=new map ();
Map.put ("A", "a"); Map.put ("B", "B"); Map.put ("C", "C")
; Map.get ("a"); Back: A
map.entryset ()//Return Entity[{key,value},{key,value}]
Map.containskey (' Kevin ')//return: false
function Map () {This.keys = new Array ();
This.data = new Object (); /** * Put a key value pair * @param {String} key * @param {Object} value */this.put = function (key, value) {I
F (this.data[key] = = null) {This.keys.push (key);
This.data[key] = value;
}else{This.data[key]=this.data[key];
return true;
}; /** * Gets the value of a key * @param {String} key * @return {Object} value */this.get = function (key) {return
This.data[key];
}; /** * Delete a key value pair * @param {String} key */this.remove = function (key) {for (Var i=0;i<this.keys.length
; i++) {if (Key===this.keys[i]) {var del_keys= this.keys.splice (i,1);
For (k in Del_keys) {this.data[k] = null;
return true;
return false;
};
/** * Traverse map, perform handler function * @param {function} callback function functions (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);
}
}; /** * Gets an array of key values * @return Entity[{key,value},{key,value}]/this.entryset = 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[this.keys[
I]]};
return entrys;
};
/** * To determine whether the map is empty/this.isempty = function () {return this.keys.length = 0;
};
/** * Gets the key value pair quantity/this.size = function () {return this.keys.length;
};
This.containskey=function (key) {return This.keys.filter (function (v) {if (V===key) {return key;
}) .length>0;
};
/** * Rewrite toString */this.tostring = function () {var s = "{";
for (Var i=0;i<this.keys.length;i++) { var k = this.keys[i];
s + + "=" +this.data[k];
if (this.keys.length>i+1) {s+= ', '}} s+= '} ';
return s;
};
/** * Parse String to map * {a=a,b=b,c=b,}/* * This.parserstringandaddmap=function (str) {var count=0;
if (str && str.length>0) {Str=str.trim ();
var startindex=str.indexof ("{"), Endindex=str.lastindexof ("}");
if (startindex!==-1 && endindex!==-1) {str=str.substring (startindex+1,endindex);
var arrs= str.split (",");
for (Var i=0;i<arrs.length;i++) {var kv=arrs[i].trim ();
if (kv.length>0 && kv.indexof ("=")!==-1) {var kv_arr=kv.split ("=");
if (kv_arr.length==2) {if (This.put (Kv_arr[0].trim (), Kv_arr[1].trim ())) {count++;
}else{console.error (' error:kv: ' +kv);
}}}}else{ Console.log ("Data error:" +STR);
}}else{Console.log (' data is not empty ');
return count;
}; }
code two:
Array.prototype.remove = function (s) {for (var i = 0; i < this.length; i++) {if (s = = This[i]) this.spli
Ce (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 () {/** The array of keys (traversal used)/This.keys = new Array ();
/** Store Data * * This.data = new Object (); /** * Put a key value pair * @param {String} key * @param {Object} value */this.put = function (key, value) {if (this
. data[key] = = null) {This.keys.push (key);
} This.data[key] = value;
}; /** * Gets the value of a key * @param {String} key * @return {Object} value */this.get = function (key) {return this.
Data[key];
};
/** * Delete a key value pair * @param {String} key */this.remove = function (key) {this.keys.remove (key);
This.data[key] = null;
}; /** * Traverse map to perform processingFunctions * * @param {function} callback function functions (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);
}
}; /** * Gets the array of key values (Java-like EntrySet ()) * @return The array of key-value objects {Key,value} */This.entrys = function () {var len = this.k
Eys.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;
};
/** * To determine whether the map is empty/this.isempty = function () {return this.keys.length = 0;
};
/** * Gets the key value pair quantity/this.size = function () {return this.keys.length;
};
/** * Rewrite toString */this.tostring = function () {var s = "{";
for (Var i=0;i<this.keys.length;i++,s+= ', ') {var k = this.keys[i];
s + + "=" +this.data[k];
} s+= "}"; RetUrn S;
}; }
function TestMap () {
var m = new Map ();
M.put (' Key1 ', ' comtop ');
M.put (' Key2 ', ' Southern Grid ');
M.put (' Key3 ', ' jingxin Garden ');
Alert ("Init:" +m);
M.put (' Key1 ', ' Kang Topu ');
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);
}
The above content through two pieces of code for everyone to share the JavaScript implementation map, I hope you like.