/** * ********* Operation Example ************** * var map = new HashMap (); * Map.put ("Key1", "Value1"); * Map.put ("Key2", "Val Ue2 "); * Map.put ("Key3", "Value3"); * Map.put ("Key4", "Value4"); * Map.put ("Key5", "Value5"); * Alert ("Size:" +map.size () + "Key1:" +map.get ("Key1")); * Map.Remove ("Key1"); * Map.put ("Key3", "newvalue"); * var values = map.values (); * for (var i in values) {* document.write (i+ ":" +values[i]+ ""); *} * document.write ("<br>"); * var keySet = Map.keyset (); * for (var i in KeySet) {* document.write (i+ ":" +keyset[i]+ ""); *} * Alert (Map.isempty ()); */ functionHashMap () {//define length varLength = 0; //Create an Object varobj =NewObject (); /** * To determine if the map is empty*/ This. IsEmpty =function(){ returnLength = = 0; }; /** * Determine if the object contains a given key*/ This. containskey=function(key) {return(Keyinchobj); }; /** * Determine if the object contains the given value*/ This. containsvalue=function(value) { for(varKeyinchobj) { if(Obj[key] = =value) { return true; } } return false; }; /** * Add data to map*/ This. put=function(key,value) {if(! This. ContainsKey (Key)) {Length++; } Obj[key]=value; }; /** * Get value based on a given key*/ This. get=function(key) {return This. ContainsKey (Key)? Obj[key]:NULL; }; /** * Delete a value based on the given key*/ This. remove=function(key) {if( This. ContainsKey (Key) && (DeleteObj[key])) {Length--; } }; /** * Get all the value in map*/ This. values=function(){ var_values=NewArray (); for(varKeyinchobj) {_values.push (Obj[key]); } return_values; }; /** * Get all the keys in the map*/ This. keyset=function(){ var_keys =NewArray (); for(varKeyinchobj) {_keys.push (key); } return_keys; }; /** * Get the length of map*/ This. Size =function(){ returnlength; }; /** * Clear Map*/ This. Clear =function() {length= 0; Obj=NewObject (); }; }
For the above code, is found on the Internet to write self-think good JS in the implementation of the HashMap example, easy to query and use later
Eg: if, as in the following circumstances, the input sorting value cannot be repeated, use the ContainsKey in the HashMap above
Specific code:
//Save$ ("#save_btn"). Click (function(){ vardatatr=$ ("#detail_table"). Find (". Tr-data")); varHasdata=false; varpasscheck=true; var temp=null;//defines a temporary variable var map = new HashMap ();//Create a HashMap object for(vari=0;i<datatr.length;i++){ var$TR =$ (datatr[i]); var$dictName = $tr. Find (' td[col= ' Dictname "]). Children (' input '); var$dispOrder = $tr. Find (' td[col= ' Disporder "]). Children (' input '); if($dictName. val () | |$dispOrder. Val ()) {HasData=true; } //checks if the input sort is repeated if($dispOrder. Val ()) { temp = $dispOrder. val ();//Gets the value of the Sort text boxif(temp== ' 0 '{Dol.alert.warning ({content:"Sort must be greater than 0" }); Passcheck=false; } if (map.size () ==0) {Map.put (temp, (i + 1) + "");//First time in, first value stored in map}else{//map value if (Map.containskey (temp)) {//determines if the map contains temp, if included, prompts repeat Dol.alert.warning ({ Content: "Sorted values are duplicates"}); Passcheck=false; return false; } else {//If no temp is included, the temp is stored in map map.put (temp, (i + 1) + ""); } } } if(!Checkrowdata ($TR)) {Passcheck=false; } } if(!Passcheck) { return; } $(' #editForm '). Ajaxsubmit ({URL:' ${basepath}/dict/dict_editdict.do ', type:' Post ', DataType:' JSON ', Success:function(data) {if(data) {if(data.issuccess) {Dol.alert.success ({content:"Modified successfully", ' OnClose ':function(){ /*window.location.href= "${basepath}/dict/dict_showdictlist.do";*/dol.reloadparent (); Window.close (); } }); }Else{Dol.alert.error ({content:"Modify Failed"}); } } }, }); DOL.DISABLEDBTN ("Save_btn"); });
Now that new HashMap, there must be hashmap.
So in JS to write
function HashMap () {
Define length
var length = 0;
Create an Object
var obj = new Object ();
/**
* Determine if the object contains a given key
*/
This.containskey=function (key) {
Return (key in obj);
};
/**
* Add data to map
*/
This.put=function (Key,value) {
if (!this.containskey (key)) {
length++;
}
Obj[key] = value;
};
/**
* Get the length of the map
*/
This.size = function () {
return length;
};
}
So you can use HashMap
JS Implementation HashMap