# ForEach with Typescript-Collection

**URL:** https://discourse.aurelia.io/t/foreach-with-typescript-collection/1182
**Category:** Help Requests
**Created:** [May 2, 2018, 7:51am UTC](https://discourse.aurelia.io/t/foreach-with-typescript-collection/1182 "2018-05-02T07:51:28Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![SNO](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sno/32/650_2.png) [@SNO](https://discourse.aurelia.io/u/SNO)
#### Post date: [May 2, 2018, 7:51am UTC](https://discourse.aurelia.io/t/foreach-with-typescript-collection/1182/1 "2018-05-02T07:51:28Z")

</div>

Hi all,

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

```auto
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:

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

```

Can anyone help me?

---

<div class="post-metadata">

### Author: ![Kukks](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/kukks/32/361_2.png) [@Kukks](https://discourse.aurelia.io/u/Kukks)
#### Post date: [May 2, 2018, 7:53am UTC](https://discourse.aurelia.io/t/foreach-with-typescript-collection/1182/2 "2018-05-02T07:53:58Z")

</div>

from their code

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

```

so this should work:

```auto
testCollection.foreach((key:string,value:string)=> {
console.log(key, value);
})

```

---

<div class="post-metadata">

### Author: ![Kukks](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/kukks/32/361_2.png) [@Kukks](https://discourse.aurelia.io/u/Kukks)
#### Post date: [May 2, 2018, 8:04am UTC](https://discourse.aurelia.io/t/foreach-with-typescript-collection/1182/3 "2018-05-02T08:04:32Z")

</div>

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

```auto
//define
const dict: { [index:string]: string} = {};
//set
dict["key"] = "value"
//remove
delete dict["key"];
dict["key1"]="value1";
dict["key2"]="value2";
//loop
Object.keys(dict).forEach(key=> {
  console.log(`key:${key}`, `val:${dict[key]}`);
});
//key check
if(dict.hasOwnproperty("key2")){
  console.log("dict has key2");
}

```

---

<div class="post-metadata">

### Author: ![SNO](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sno/32/650_2.png) [@SNO](https://discourse.aurelia.io/u/SNO)
#### Post date: [May 2, 2018, 8:11am UTC](https://discourse.aurelia.io/t/foreach-with-typescript-collection/1182/4 "2018-05-02T08:11:17Z")

</div>

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

```auto
myMethod(testCollection: ???){}

```

Would you set the type to any?

---

<div class="post-metadata">

### Author: ![Kukks](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/kukks/32/361_2.png) [@Kukks](https://discourse.aurelia.io/u/Kukks)
#### Post date: [May 2, 2018, 8:35am UTC](https://discourse.aurelia.io/t/foreach-with-typescript-collection/1182/5 "2018-05-02T08:35:49Z")

</div>

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