ForEach with Typescript-Collection

Hi all,

I guess I have an easy question, but where I couldn’t find a good solution for.
I’m using typescript-collections:

let testCollection = Collections.Dictionary<string, Collection<string, string>();

Now I want to iterate all elements. For that, the collection provides a foreach-method. But I don’t have any idea how I can get the key and value within the foreach method:

testCollection.foreach(element => {
    let key: string = ???
    let value: Collection.Dictionary<string, string> = ???

Can anyone help me?

from their code

   forEach(callback: (key: K, value: V) => any): void 

so this should work:

testCollection.foreach((key:string,value:string)=> {
console.log(key, value);
1 Like

On another note, you can define dictionaries without any third party libraries.

const dict: { [index:string]: string} = {};
dict["key"] = "value"
delete dict["key"];
Object.keys(dict).forEach(key=> {
  console.log(`key:${key}`, `val:${dict[key]}`);
//key check
  console.log("dict has key2");
1 Like

One additional question…
How do you recommend to set the method parameter expecting that kind of collection?

myMethod(testCollection: ???){}

Would you set the type to any?

I would avoid any until it becomes too troublesome.
Looking at the source it should just be
myMethod(testCollection: Dictionary<string, string>){}