JS Implementation key Value form

Source: Internet
Author: User

Way One
Java code
  1. var hashMap = {};
  2.       
  3. hashmap[' key1 '] = ' value1 ';
  4. hashmap[' Key2 '] = ' value2 ';
  5. hashmap[' Key3 '] = ' value3 ';
  6. hashmap[' Key4 '] = ' value4 ';
  7. hashmap[' key5 '] = ' value5 ';
  8. hashmap[' Key6 '] = ' value6 ';
  9.       
  10. if( ' Key2 ' in hashMap)
  11.     {  
  12. alert (' existance ');
  13.     }  
  14.       
  15. For (value in HashMap)
  16.     {  
  17. alert (value);
  18.     }  


Mode 2

JS Code
  1. var hashMap = {
  2. Set: function(key,value) {this[key] = value},
  3. Get: function(key) {return this[key]},
  4. Contains: function(key) {return this. Get (key) = = null? false : true },     
  5. Remove: function(key) {delete this[key]}
  6. }
  7. ///////////
  8.  such as var key = "Name"; var = "AAA"; var o = {key:value;} In this case, get the result is {key: "AAA"}, I want the result is {name: "AAA"}, should be how to write 
     In my project key is indeterminate, Can only use the variable 1 floor your wrong, I want {name: "AAA"} This result 
    var key = "Name"; var val = "aaa"; var o = {};str = "O." +key+ "= '" +val+ "'"; eval (str); alert (o.name); 
     &NBSP in Object, by assigning a value     var obj = new  object ();
    Obj[key] After the  = value;  is assigned, all of the values can be traversed through for  (var key in obj).

    But no value is obtained directly through Obj[key].    such as  var  key=  ' AAA '; obj[key]  but not value    why is this?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.