在Javascript中实现哈希表



根据setter类中的内容,我在编写哈希类的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]]);

似乎给了我想要的结果

最新更新