Javascript HashTable use Object key

后端 未结 11 1264
误落风尘
误落风尘 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条回答
  •  猫巷女王i
    2020-12-13 18:22

    Inspired by @florian, here's a way where the id doesn't need JSON.stringify:

    'use strict';
    
    module.exports = HashTable;
    
    function HashTable () {
      this.index = [];
      this.table = [];
    }
    
    HashTable.prototype = {
    
      constructor: HashTable,
    
      set: function (id, key, value) {
        var index = this.index.indexOf(id);
        if (index === -1) {
          index = this.index.length;
          this.index.push(id);
          this.table[index] = {};
        }
        this.table[index][key] = value;
      },
    
      get: function (id, key) {
        var index = this.index.indexOf(id);
        if (index === -1) {
          return undefined;
        }
        return this.table[index][key];
      }
    
    };
    

提交回复
热议问题