【问题标题】:Get a value of a HashTable获取 HashTable 的值
【发布时间】:2020-12-17 00:14:02
【问题描述】:

我正在制作一个 HashTable 作为示例并保存它以解决任何问题,但我在尝试实现一个返回 true 或 false 的方法时遇到了问题,以防该值属于 HashTable,因为它在里面对象数组作为代码中的注释。

我尝试过 for 循环、.map 和 for of,但如果有人可以帮助我,它总是失败。

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}]
}

【问题讨论】:

  • 您知道对象本质上是哈希表,对吧?因此,您正在使用哈希表来实现哈希表的存储桶。

标签: javascript object hashtable


【解决方案1】:

您可以使用Object.values() 获取存储桶中所有属性的值。

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));

【讨论】:

    猜你喜欢
    • 2016-09-26
    • 2020-02-20
    • 1970-01-01
    • 2011-02-24
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 2015-10-07
    相关资源
    最近更新 更多