are there dictionaries in javascript like python?

前端 未结 7 756
轻奢々
轻奢々 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:21

    An old question but I recently needed to do an AS3>JS port, and for the sake of speed I wrote a simple AS3-style Dictionary object for JS:

    http://jsfiddle.net/MickMalone1983/VEpFf/2/

    If you didn't know, the AS3 dictionary allows you to use any object as the key, as opposed to just strings. They come in very handy once you've found a use for them.

    It's not as fast as a native object would be, but I've not found any significant problems with it in that respect.

    API:

    //Constructor
    var dict = new Dict(overwrite:Boolean);
    
    //If overwrite, allows over-writing of duplicate keys,
    //otherwise, will not add duplicate keys to dictionary.
    
    dict.put(key, value);//Add a pair
    dict.get(key);//Get value from key
    dict.remove(key);//Remove pair by key
    dict.clearAll(value);//Remove all pairs with this value
    dict.iterate(function(key, value){//Send all pairs as arguments to this function:
        console.log(key+' is key for '+value);
    });
    
    
    dict.get(key);//Get value from key
    

提交回复
热议问题