Home > database >  2 keys to a value in dictionary typescript
2 keys to a value in dictionary typescript

Time:12-08

I have a dictionary which will show result based on the key value. I would like to read the 2 keys and show the result based on the 2 key values. currently x['1'] = 2. The result I would like to have x['a']['1'] = 2.

dict: { [letters: string] : resultmodel;} = {};

CodePudding user response:

If you are after this syntax x['a']['1'] = 2, then it would be a dictionary within a dictionary, like this:

const dict: { [key: string]: { [key]: any } } = {
    ['a']: { ['b']: 'test' }
};
console.log(dict['a']['b']);

But you can also create a dictionary with a composite key so that your syntax would be dict['a', 'b'], and it would look like this:

const dict2: { [key1: string, key2: string]: any } = {
    ['a', 'b']: 'test'
};
console.log(dict2['a','b']);
  • Related