用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,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,value2]]);

似乎给了我想要的结果