I am looking to implement a Dictionary class in my application. I have created a class with an Array of KeyValuePair to store my list.
export class KeyValuePair<TKey, TVal>{
key:TKey;
value:TVal;
constructor(key:TKey, val:TVal){
this.key = key;
this.value = val;
}
export class Dictionary<TKey, TVal>{
array: Array<KeyValuePair<TKey, TVal>>
}
let myClassInstance:Dictionary<number, string> = ...;
Some Questions:
- I want to be able to iterate through it using forEach or in a loop like 'let x of myClasInstance', how can I achieve this? (myClassInstance.forEach(...);)
- Can I access the array in the class instance without referring to className.arrayName directly? (myClassInstance.find(...);)
- Is it possible to use the class instance as an index to retrieve values? (myClassInstance[1])
- If there is a better structure for this implementation, I would love to hear and learn about it. Thank you!!!