Я искал аналогичную структуру данных, но не смог найти. Я работал с TypeScript, поэтому я разработал решение с использованием 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];
});
} }
Вы можете просто создать карту с двумя ключами, как показано ниже:
let twoKeyMap = new TwoKeyMap<any>();
twoKeyMap.set('mainKey1', 'nestedKey1', {value: 'value'});
его не так ЭФФЕКТИВНАЯ как HashMap. Вы должны иметь возможность конвертировать то же самое в ES6 или ES5 легко.
Если у вас есть ключи как строки и вы можете убедиться, что они не содержат конкретных символов, таких как '|', вы можете использовать что-то вроде 'map [a + '|' + b] ' – zzzzBov