Javascript HashTable use Object key

后端 未结 11 1236
误落风尘
误落风尘 2020-12-13 17:29

I want to create a hash table with Object keys that are not converted into String.

Some thing like this:

var object1 = new Object();
var o         


        
11条回答
  •  伪装坚强ぢ
    2020-12-13 18:30

    Here is a proposal, combining @Florian's solution with @Laurent's.

    function HashTable() {
        this.hashes = [];
    }
    
    HashTable.prototype = {
        constructor: HashTable,
    
        put: function( key, value ) {
            this.hashes.push({
                key: key,
                value: value
            });
        },
    
        get: function( key ) {
            for( var i = 0; i < this.hashes.length; i++ ){
                if(this.hashes[i].key == key){
                    return this.hashes[i].value;
                }
            }
        }
    };
    

    It wont change your object in any way and it doesn't rely on JSON.stringify.

提交回复
热议问题