Dictionary is a useful tool that has been used frequently in previous C # projects, this post is mainly about JavaScript, the actual application scenario of the dictionary
First in JS, there is no dictionary ' class, we need to implement the key value (key)-value (value) of the corresponding relationship, the best way is to use the array
First of all, the background, I need to base on the site number in the array to get to the corresponding site name
First, through the thrift interface, get the device information in the database, through the AJAX transfer to the JS page, traversing the array
PS: Data structure is as follows:
# device Information struct Device{int id,# string name, ...};
# site Information struct site{int id,# string name,.....,list<device> lsdevices};
# zone information struct Area{int id,# string name,.....,list<site> lssites};
First get to the region information, and then according to the length of list<site> lssites to determine if there is no site, if there is, get information, and so on
I found a csdn above, the URL is as follows: Http://www.cnblogs.com/baiyangyuanzi/p/6689554.html, on his basis, I made a slight modification, extracted the part I need
The code is as follows:
1 functionDictionary () {2 This. Datastore =NewArray ();3 This. Add =function(key, value) {4 This. Datastore[key] =value;5 }6 This. Find =function(key) {7 return This. Datastore[key];8 }9}
View Code
Then instantiate the Dictionary class
var adddevide = new Dictionary ();
Start the convenience array, pass the value, the code is as follows:
1 //Device number Dictionary2 functionDictionarydevice (list) {3 varLength =list.length;4 //Storage Device ID5 varID = "";6 //Storage Device Name7 varName = "";8 9 vari = 0;Ten varII = 0; One varIII = 0; A - for(i = 0;i<length;i++){ -ID =List[i].szareaid; theName =List[i].szareaname; - Adddevide.add (id,name); - if(List[i].lssites.length = = 0){ - Break; + } - Else{ + for(ii = 0;ii<list[i].lssites.length;ii++){ AID =List[i].lssites[ii].szsiteid; atName =List[i].lssites[ii].szsitename; - Adddevide.add (id,name); - if(List[i].lssites[ii].lsdevices.length = = 0){ - Break; - } - Else{ in for(iii = 0;iii<list[i].lssites[ii].lsdevices.length;iii++) { -ID =List[i].lssites[ii].lsdevices[iii].szdeviceid; toName =List[i].lssites[ii].lsdevices[iii].szdevicename; + Adddevide.add (ID, Name); - } the } * } $ }Panax Notoginseng } - theAlert (Adddevide.find ("JS0101")); +}
View Code
Run the program, appear JS0101 corresponding to the site, the dictionary completed
Javascript Dictionary Application Example