I was looking for a similar data structure, but could not find. I have been working with TypeScript, so I have developed a solution using TypeScript.
export class TwoMapKey<T> {
private __map__: object;
constructor() {
this.__map__ = new Object();
this.get = this.get.bind(this);
this.set = this.set.bind(this);
this.remove = this.remove.bind(this);
this.keys = this.keys.bind(this);
this.nestedKeys = this.nestedKeys.bind(this);
this.clear = this.clear.bind(this);
}
public get(key: string, nestedKey: string): T {
if (!this.__map__[key] || this.__map__[key] && !this.__map__[key][nestedKey])
return;
return this.__map__[key][nestedKey];
}
public set(key: string, nestedKey: string, value: T): void {
if (!this.__map__[key]) {
this.__map__[key] = new Object();
}
Object.defineProperty(this.__map__[key], nestedKey, { value: value, configurable: true, enumerable: true });
}
public remove(key, nestedKey): void {
if (!this.__map__[key]) {
return;
}
delete this.__map__[key][nestedKey];
}
public keys(): string[] {
return Object.getOwnPropertyNames(this.__map__);
}
public nestedKeys(): Array<string[]> {
return Object.getOwnPropertyNames(this.__map__).map(key => Object.keys(this.__map__[key]));
}
public clear(): void {
Object.getOwnPropertyNames(this.__map__).forEach(property => {
delete this.__map__[property];
});
} }
You can simply create a map with two keys as below:
let twoKeyMap = new TwoKeyMap<any>();
twoKeyMap.set('mainKey1', 'nestedKey1', {value: 'value'});
Its not as efficent as a HashMap. You should be able to convert the same in ES6 or ES5 easily.