are there dictionaries in javascript like python?

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

    Have created a simple dictionary in JS here:

    function JSdict() {
        this.Keys = [];
        this.Values = [];
    }
    
    // Check if dictionary extensions aren't implemented yet.
    // Returns value of a key
    if (!JSdict.prototype.getVal) {
        JSdict.prototype.getVal = function (key) {
            if (key == null) {
                return "Key cannot be null";
            }
            for (var i = 0; i < this.Keys.length; i++) {
                if (this.Keys[i] == key) {
                    return this.Values[i];
                }
            }
            return "Key not found!";
        }
    }
    
    
    // Check if dictionary extensions aren't implemented yet.
    // Updates value of a key
    if (!JSdict.prototype.update) {
        JSdict.prototype.update = function (key, val) {
            if (key == null || val == null) {
                return "Key or Value cannot be null";
            }
            // Verify dict integrity before each operation
            if (keysLength != valsLength) {
                return "Dictionary inconsistent. Keys length don't match values!";
            }
            var keysLength = this.Keys.length;
            var valsLength = this.Values.length;
            var flag = false;
            for (var i = 0; i < keysLength; i++) {
                if (this.Keys[i] == key) {
                    this.Values[i] = val;
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                return "Key does not exist";
            }
        }
    }
    
    
    
    // Check if dictionary extensions aren't implemented yet.
    // Adds a unique key value pair
    if (!JSdict.prototype.add) {
        JSdict.prototype.add = function (key, val) {
            // Allow only strings or numbers as keys
            if (typeof (key) == "number" || typeof (key) == "string") {
                if (key == null || val == null) {
                    return "Key or Value cannot be null";
                }
                if (keysLength != valsLength) {
                    return "Dictionary inconsistent. Keys length don't match values!";
                }
                var keysLength = this.Keys.length;
                var valsLength = this.Values.length;
                for (var i = 0; i < keysLength; i++) {
                    if (this.Keys[i] == key) {
                        return "Duplicate keys not allowed!";
                    }
                }
                this.Keys.push(key);
                this.Values.push(val);
            }
            else {
                return "Only number or string can be key!";
            }
        }
    }
    
    // Check if dictionary extensions aren't implemented yet.
    // Removes a key value pair
    if (!JSdict.prototype.remove) {
        JSdict.prototype.remove = function (key) {
            if (key == null) {
                return "Key cannot be null";
            }
            if (keysLength != valsLength) {
                return "Dictionary inconsistent. Keys length don't match values!";
            }
            var keysLength = this.Keys.length;
            var valsLength = this.Values.length;
            var flag = false;
            for (var i = 0; i < keysLength; i++) {
                if (this.Keys[i] == key) {
                    this.Keys.shift(key);
                    this.Values.shift(this.Values[i]);
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                return "Key does not exist";
            }
        }
    }
    

    The above implementation can now be used to simulate a dictionary as:

    var dict = new JSdict();
    
    dict.add(1, "one")
    
    dict.add(1, "one more")
    "Duplicate keys not allowed!"
    
    dict.getVal(1)
    "one"
    
    dict.update(1, "onne")
    
    dict.getVal(1)
    "onne"
    
    dict.remove(1)
    
    dict.getVal(1)
    "Key not found!"
    

    This is just a basic simulation. It can be further optimized by implementing a better running time algorithm to work in atleast O(nlogn) time complexity or even less. Like merge/quick sort on arrays and then some B-search for lookups. I Didn't give a try or searched about mapping a hash function in JS.

    Also, Key and Value for the JSdict obj can be turned into private variables to be sneaky.

    Hope this helps!

    EDIT >> After implementing the above, I personally used the JS objects as associative arrays that are available out-of-the-box.

    However, I would like to make a special mention about two methods that actually proved helpful to make it a convenient hashtable experience.

    Viz: dict.hasOwnProperty(key) and delete dict[key]

    Read this post as a good resource on this implementation/usage. Dynamically creating keys in JavaScript associative array

    THanks!

提交回复
热议问题