How to properly iterate over a Map in TypeScript? All methods and examples
Hello everyone! I've just started learning TypeScript and encountered a problem when working with Map. I know that Map is a convenient collection for storing key-value pairs, but I can't figure out how to iterate over it properly.
In JavaScript, I'm used to using `for...of` for arrays, but with Map it's somehow confusing. I tried different options, but TypeScript complains about types or gives me something I don't expect.
Here's an example of my code:
```typescript
const userScores = new Map();
userScores.set('Alice', 95);
userScores.set('Bob', 87);
userScores.set('Charlie', 92);
// Trying to iterate — doesn't work
for (const entry of userScores) {
console.log(entry); // What will this be? An array? An object?
}
// Tried using .entries() — also unclear
const entries = userScores.entri
No answers yet.
Log in to leave an answer