HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptCritical

TypeScript, Looping through a dictionary

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
typescriptdictionarythroughlooping

Problem

In my code, I have a couple of dictionaries (as suggested here) which is String indexed. Due to this being a bit of an improvised type, I was wondering if there any suggestions on how I would be able to loop through each key (or value, all I need the keys for anyway). Any help appreciated!

myDictionary: { [index: string]: any; } = {};

Solution

To loop over the key/values, use a for in loop:

for (let key in myDictionary) {
    let value = myDictionary[key];
    // Use `key` and `value`
}

Code Snippets

for (let key in myDictionary) {
    let value = myDictionary[key];
    // Use `key` and `value`
}

Context

Stack Overflow Q#16174182, score: 467

Revisions (0)

No revisions yet.