Search code examples
javascriptobjecthashtable

Get a value of a HashTable


I was making a HashTable to have as an example and have it saved for any problem, but I ran into a problem trying to implement a method that returns true or false in case the value belongs to the HashTable, since it is inside a arrays of objects as comment in the code.

I have tried for loops, .map and for of, but it always fails, if someone could help me.

function HashTable () {
    this.buckets = [];
    this.numbuckets = 35;
}

HashTable.prototype.hash = function (key) {
    let suma = 0;
    for (let i = 0; i < key.length; i++) {
        suma = suma + key.charCodeAt(i);
    }
    return suma % this.numbuckets;
}

HashTable.prototype.set = function (key, value) {
    if (typeof key !== "string") {
        throw new TypeError ("Keys must be strings")
    } else {
        var index = this.hash(key);
        if(this.buckets[index] === undefined) {
            this.buckets[index] = {};
        }
        this.buckets[index][key] = value;
    }
}

HashTable.prototype.get = function (key) {
    var index = this.hash(key);
    return this.buckets[index][key];
}

HashTable.prototype.hasKey = function (key) {
    var index = this.hash(key);
    return this.buckets[index].hasOwnProperty(key)
}

HashTable.prototype.remove = function (key) {
    var index = this.hash(key);
    if (this.buckets[index].hasOwnProperty(key)) {
       delete this.buckets[index]
       return true;
    }
    return false;
}

HashTable.prototype.hasValue = function (value) {
    let result = this.buckets;
    result = result.flat(Infinity);
    
    return result // [{Name: Toni}, {Mame: Tino}, {Answer: Jhon}]
}


Solution

  • You can use Object.values() to get the values of all the propertyies in the bucket.

    function HashTable() {
      this.buckets = [];
      this.numbuckets = 35;
    }
    
    HashTable.prototype.hash = function(key) {
      let suma = 0;
      for (let i = 0; i < key.length; i++) {
        suma = suma + key.charCodeAt(i);
      }
      return suma % this.numbuckets;
    }
    
    HashTable.prototype.set = function(key, value) {
      if (typeof key !== "string") {
        throw new TypeError("Keys must be strings")
      } else {
        var index = this.hash(key);
        if (this.buckets[index] === undefined) {
          this.buckets[index] = {};
        }
        this.buckets[index][key] = value;
      }
    }
    
    HashTable.prototype.get = function(key) {
      var index = this.hash(key);
      return this.buckets[index][key];
    }
    
    HashTable.prototype.hasKey = function(key) {
      var index = this.hash(key);
      return this.buckets[index].hasOwnProperty(key)
    }
    
    HashTable.prototype.remove = function(key) {
      var index = this.hash(key);
      if (this.buckets[index].hasOwnProperty(key)) {
        delete this.buckets[index]
        return true;
      }
      return false;
    }
    
    HashTable.prototype.hasValue = function(value) {
      return this.buckets.some(bucket => Object.values(bucket).includes(value));
    }
    
    let h = new HashTable;
    h.set("Abc", 1);
    h.set("Def", 2);
    console.log(h.hasValue(1));
    console.log(h.hasValue(3));