Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 17
    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.
    – Sat Thiru
    Commented May 8, 2019 at 14:52
  • 13
    @SatThiru An array of tuples is the customary representation of Maps, 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.
    – Bergi
    Commented May 8, 2019 at 16:42
  • Bergi, please note that OP said "My keys are guaranteed to be strings".
    – Sat Thiru
    Commented May 10, 2019 at 17:20
  • 14
    @SatThiru In that case, use JSON.stringify(Object.fromEntries(map.entries())) and new Map(Object.entries(JSON.parse(jsonText)))
    – Bergi
    Commented May 10, 2019 at 17:43
  • 6
    @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.
    – Bergi
    Commented Mar 13, 2020 at 18:22