I've got a Map<string, string>
variable in typescript:
let m = Map<string, string>().set('tag', 'v1');
I want to convert to json string representation:
'{"tag": "v1"}'
I've tried 3 different ways. First is to use m.toString()
. Second is using JSON.stringify(m)
. Both returned {}
. I've even tried to convert the Map
to a javascript object
first and then convert to string:
function MapToString(map): string {
let ro = {};
Object.keys(map).forEach( key => {
ro[key] = map[key];
});
return JSON.stringify(ro);
}
s = MapToString(m);
This returned {}
as well when I tried to print it in the console.