are there dictionaries in javascript like python?

前端 未结 7 736
轻奢々
轻奢々 2020-12-07 18:59

i need to make a dictionary in javascript like this

i dont remember the exact notation, but it was something like:

states_dictionary={ CT=[alex,harry         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-07 19:11

    This is an old post, but I thought I should provide an illustrated answer anyway.

    Use javascript's object notation. Like so:

    states_dictionary={ 
         "CT":["alex","harry"], 
         "AK":["liza","alex"], 
         "TX":["fred", "harry"]
    };
    

    And to access the values:

    states_dictionary.AK[0] //which is liza
    

    or you can use javascript literal object notation, whereby the keys not require to be in quotes:

    states_dictionary={ 
         CT:["alex","harry"], 
         AK:["liza","alex"], 
         TX:["fred", "harry"]
    };
    

提交回复
热议问题