Iterating over Typescript Map
This worked for me. TypeScript Version: 2.8.3
for (const [key, value] of Object.entries(myMap)) {
console.log(key, value);
}
You could use Map.prototype.forEach((value, key, map) => void, thisArg?) : void
instead
Use it like this:
myMap.forEach((value: boolean, key: string) => {
console.log(key, value);
});
es6
for (let [key, value] of map) {
console.log(key, value);
}
es5
for (let entry of Array.from(map.entries())) {
let key = entry[0];
let value = entry[1];
}
Just use Array.from()
method to convert it to an Array
:
myMap : Map<string, boolean>;
for(let key of Array.from( myMap.keys()) ) {
console.log(key);
}