javascript - 在Javascript中实现哈希表
问题描述
根据我在 setter 类中的内容,我无法编写我的 hash 类的 getter 方法。
我得到的错误是:error: Uncaught TypeError: Cannot read property '1' of undefined
setItem = (key, value, value2) => {
const idx = HashStringToInt(key, this.table.length);
if (this.table[idx]) {
this.table.push([key,[value, value2]]);
} else {
this.table[idx] = [[key, [value, value2]]]
}
}
getItem = key => {
const idx = HashStringToInt(key, this.table.length);
if (!this.table[idx]) {
return null;
}
return this.table[idx].find(x => x[0] === key)[1]; //this doesn't work
}
解决方案
改变:
this.table.push([key,[value, value2]]);
至:
this.table[idx].push([key,[value, value2]]);
似乎给了我想要的结果
推荐阅读
- powershell - PowerShell - 查找具有特定 AD 属性的 AD 用户
- php - Laravel / IMPALA:如何进行参数化查询?
- arm - Cortex-M0 硬故障处理程序在更改后不会被调用
- wordpress - WooCommerce Paypal - 不允许基于 PayPal 帐单地址的付款(国家/地区限制)
- google-apps-script - 在与当前文件相同的文件夹中创建一个谷歌电子表格
- google-chrome - 当我在 v86 中打开开发工具时,Chrome 在刷新页面后不会释放内存
- reactjs - 将 Create-React-App 与 Apache VirtualHost 一起使用
- wcf-ria-services - OpenRiaServices 异步方法
- powershell - 是否可以在 PSObject 中为“for”做一个“for”?
- javascript - 使用 Javascript 访问 iFrame 中的 DOM