Timeline for How do you JSON.stringify an ES6 Map?
Current License: CC BY-SA 4.0
6 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 13, 2020 at 18:22 | comment | added | Bergi |
@Drenai Then don't use Obect.fromEntries , and use the code from my main answer instead of the one from the comment. The code that builds an object literal was in response to Sat Thiru, who gave the case that the keys are strings.
|
|
May 10, 2019 at 17:43 | comment | added | Bergi |
@SatThiru In that case, use JSON.stringify(Object.fromEntries(map.entries())) and new Map(Object.entries(JSON.parse(jsonText)))
|
|
May 10, 2019 at 17:20 | comment | added | Sat Thiru | Bergi, please note that OP said "My keys are guaranteed to be strings". | |
May 8, 2019 at 16:42 | comment | added | Bergi |
@SatThiru An array of tuples is the customary representation of Map s, it goes well with the constructor and iterator. Also it is the only sensible representation of maps that have non-string keys, and object would not work there.
|
|
May 8, 2019 at 14:52 | comment | added | Sat Thiru | This does not convert to a JSON object, but instead to an Array of arrays. Not the same thing. See Evan Carroll's answer below for a more complete answer. | |
Nov 24, 2018 at 19:10 | history | answered | Bergi | CC BY-SA 4.0 |