Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  39] [ 1]  / answers: 1 / hits: 35372  / 7 Years ago, wed, september 6, 2017, 12:00:00

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.


More From » json

 Answers
16

I eventually gave up on using es6 Map and switched to TSMap, on which tags.toJSON() works.


[#56566] Sunday, September 3, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
peytont

Total Points: 215
Total Questions: 110
Total Answers: 111

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
;