How do you JSON.stringify an ES6 Map?












56















I'd like to start using ES6 Map instead of JS objects but I'm being held back because I can't figure out how to JSON.stringify() a Map. My keys are guaranteed to be strings and my values will always be lists. Do I really have to write a wrapper method to serialize?










share|improve this question























  • interesting article on the topic 2ality.com/2015/08/es6-map-json.html

    – David Chase
    Apr 4 '18 at 15:58











  • I was able to get this to work. The results are on Plunkr at embed.plnkr.co/oNlQQBDyJUiIQlgWUPVP. The solution uses a JSON.stringify(obj, replacerFunction) which checks to see if a Map object is being passed and converts the Map object to a Javascript object (that JSON.stringify) will then convert to a string.

    – PatS
    Apr 10 '18 at 22:05






  • 1





    If your keys are guaranteed to be strings (or numbers) and your values arrays, you can do something like [...someMap.entries()].join(';'); for something more complex you could try something similar using something like [...someMap.entries()].reduce((acc, cur) => acc + `${cur[0]}:${/* do something to stringify cur[1] */ }`, '')

    – user568458
    May 10 '18 at 8:42













  • @Oriol What if it is possible for key name to be same as default properties? obj[key] may get you something unexpected. Consider the case if (!obj[key]) obj[key] = newList; else obj[key].mergeWith(newList);.

    – Franklin Yu
    Nov 20 '18 at 15:20











  • @Brad Why did you reopen this?

    – Bergi
    Nov 24 '18 at 18:46
















56















I'd like to start using ES6 Map instead of JS objects but I'm being held back because I can't figure out how to JSON.stringify() a Map. My keys are guaranteed to be strings and my values will always be lists. Do I really have to write a wrapper method to serialize?










share|improve this question























  • interesting article on the topic 2ality.com/2015/08/es6-map-json.html

    – David Chase
    Apr 4 '18 at 15:58











  • I was able to get this to work. The results are on Plunkr at embed.plnkr.co/oNlQQBDyJUiIQlgWUPVP. The solution uses a JSON.stringify(obj, replacerFunction) which checks to see if a Map object is being passed and converts the Map object to a Javascript object (that JSON.stringify) will then convert to a string.

    – PatS
    Apr 10 '18 at 22:05






  • 1





    If your keys are guaranteed to be strings (or numbers) and your values arrays, you can do something like [...someMap.entries()].join(';'); for something more complex you could try something similar using something like [...someMap.entries()].reduce((acc, cur) => acc + `${cur[0]}:${/* do something to stringify cur[1] */ }`, '')

    – user568458
    May 10 '18 at 8:42













  • @Oriol What if it is possible for key name to be same as default properties? obj[key] may get you something unexpected. Consider the case if (!obj[key]) obj[key] = newList; else obj[key].mergeWith(newList);.

    – Franklin Yu
    Nov 20 '18 at 15:20











  • @Brad Why did you reopen this?

    – Bergi
    Nov 24 '18 at 18:46














56












56








56


5






I'd like to start using ES6 Map instead of JS objects but I'm being held back because I can't figure out how to JSON.stringify() a Map. My keys are guaranteed to be strings and my values will always be lists. Do I really have to write a wrapper method to serialize?










share|improve this question














I'd like to start using ES6 Map instead of JS objects but I'm being held back because I can't figure out how to JSON.stringify() a Map. My keys are guaranteed to be strings and my values will always be lists. Do I really have to write a wrapper method to serialize?







javascript ecmascript-6 io.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 16 '15 at 19:19









rynoprynop

23k196768




23k196768













  • interesting article on the topic 2ality.com/2015/08/es6-map-json.html

    – David Chase
    Apr 4 '18 at 15:58











  • I was able to get this to work. The results are on Plunkr at embed.plnkr.co/oNlQQBDyJUiIQlgWUPVP. The solution uses a JSON.stringify(obj, replacerFunction) which checks to see if a Map object is being passed and converts the Map object to a Javascript object (that JSON.stringify) will then convert to a string.

    – PatS
    Apr 10 '18 at 22:05






  • 1





    If your keys are guaranteed to be strings (or numbers) and your values arrays, you can do something like [...someMap.entries()].join(';'); for something more complex you could try something similar using something like [...someMap.entries()].reduce((acc, cur) => acc + `${cur[0]}:${/* do something to stringify cur[1] */ }`, '')

    – user568458
    May 10 '18 at 8:42













  • @Oriol What if it is possible for key name to be same as default properties? obj[key] may get you something unexpected. Consider the case if (!obj[key]) obj[key] = newList; else obj[key].mergeWith(newList);.

    – Franklin Yu
    Nov 20 '18 at 15:20











  • @Brad Why did you reopen this?

    – Bergi
    Nov 24 '18 at 18:46



















  • interesting article on the topic 2ality.com/2015/08/es6-map-json.html

    – David Chase
    Apr 4 '18 at 15:58











  • I was able to get this to work. The results are on Plunkr at embed.plnkr.co/oNlQQBDyJUiIQlgWUPVP. The solution uses a JSON.stringify(obj, replacerFunction) which checks to see if a Map object is being passed and converts the Map object to a Javascript object (that JSON.stringify) will then convert to a string.

    – PatS
    Apr 10 '18 at 22:05






  • 1





    If your keys are guaranteed to be strings (or numbers) and your values arrays, you can do something like [...someMap.entries()].join(';'); for something more complex you could try something similar using something like [...someMap.entries()].reduce((acc, cur) => acc + `${cur[0]}:${/* do something to stringify cur[1] */ }`, '')

    – user568458
    May 10 '18 at 8:42













  • @Oriol What if it is possible for key name to be same as default properties? obj[key] may get you something unexpected. Consider the case if (!obj[key]) obj[key] = newList; else obj[key].mergeWith(newList);.

    – Franklin Yu
    Nov 20 '18 at 15:20











  • @Brad Why did you reopen this?

    – Bergi
    Nov 24 '18 at 18:46

















interesting article on the topic 2ality.com/2015/08/es6-map-json.html

– David Chase
Apr 4 '18 at 15:58





interesting article on the topic 2ality.com/2015/08/es6-map-json.html

– David Chase
Apr 4 '18 at 15:58













I was able to get this to work. The results are on Plunkr at embed.plnkr.co/oNlQQBDyJUiIQlgWUPVP. The solution uses a JSON.stringify(obj, replacerFunction) which checks to see if a Map object is being passed and converts the Map object to a Javascript object (that JSON.stringify) will then convert to a string.

– PatS
Apr 10 '18 at 22:05





I was able to get this to work. The results are on Plunkr at embed.plnkr.co/oNlQQBDyJUiIQlgWUPVP. The solution uses a JSON.stringify(obj, replacerFunction) which checks to see if a Map object is being passed and converts the Map object to a Javascript object (that JSON.stringify) will then convert to a string.

– PatS
Apr 10 '18 at 22:05




1




1





If your keys are guaranteed to be strings (or numbers) and your values arrays, you can do something like [...someMap.entries()].join(';'); for something more complex you could try something similar using something like [...someMap.entries()].reduce((acc, cur) => acc + `${cur[0]}:${/* do something to stringify cur[1] */ }`, '')

– user568458
May 10 '18 at 8:42







If your keys are guaranteed to be strings (or numbers) and your values arrays, you can do something like [...someMap.entries()].join(';'); for something more complex you could try something similar using something like [...someMap.entries()].reduce((acc, cur) => acc + `${cur[0]}:${/* do something to stringify cur[1] */ }`, '')

– user568458
May 10 '18 at 8:42















@Oriol What if it is possible for key name to be same as default properties? obj[key] may get you something unexpected. Consider the case if (!obj[key]) obj[key] = newList; else obj[key].mergeWith(newList);.

– Franklin Yu
Nov 20 '18 at 15:20





@Oriol What if it is possible for key name to be same as default properties? obj[key] may get you something unexpected. Consider the case if (!obj[key]) obj[key] = newList; else obj[key].mergeWith(newList);.

– Franklin Yu
Nov 20 '18 at 15:20













@Brad Why did you reopen this?

– Bergi
Nov 24 '18 at 18:46





@Brad Why did you reopen this?

– Bergi
Nov 24 '18 at 18:46












3 Answers
3






active

oldest

votes


















37














You can't.



The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.




My keys are guaranteed to be strings and my values will always be lists




In this case, you can use a plain object. It will have these advantages:




  • It will be able to be stringified to JSON.

  • It will work on older browsers.

  • It might be faster.






share|improve this answer





















  • 6





    for the curious-in the latest chrome, any map serializes into '{}'

    – Capaj
    Jan 6 '16 at 16:20











  • I have explained here what exactly I meant when I said "you can't".

    – Oriol
    Jan 29 '16 at 6:20






  • 5





    "It might be faster" - Do you have any source on that? I'm imagining a simple hash-map must be faster than a full blown object, but I have no proof. :)

    – Lilleman
    Feb 11 '16 at 18:01






  • 1





    @Xplouder That test uses expensive hasOwnProperty. Without that, Firefox iterates objects much faster than maps. Maps are still faster on Chrome, though. jsperf.com/es6-map-vs-object-properties/95

    – Oriol
    Mar 18 '16 at 13:49






  • 1





    True, seems that Firefox 45v iterates objects away faster than Chrome +49v. However Maps still wins vs objects in Chrome.

    – Xplouder
    Mar 18 '16 at 19:52



















8














You can't directly stringify the Map instance as it doesn't have any properties, but you can convert it to an array of tuples:



jsonText = JSON.stringify(Array.from(map.entries()));


For the reverse, use



map = new Map(JSON.parse(jsonText));





share|improve this answer































    1














    While there is no method provided by ecmascript yet, this can still be done using JSON.stingify if you map the Map to a JavaScript primitive. Here is the sample Map we'll use.



    const map = new Map();
    map.set('foo', 'bar');
    map.set('baz', 'quz');


    Going to an JavaScript Object



    You can convert to JavaScript Object literal with the following helper function.



    const mapToObj = m => {
    return Array.from(m).reduce((obj, [key, value]) => {
    obj[key] = value;
    return obj;
    }, {});
    };

    JSON.stringify(mapToObj(map)); // '{"foo":"bar","baz":"quz"}'


    Going to a JavaScript Array of Objects



    The helper function for this one would be even more compact



    const mapToAoO = m => {
    return Array.from(m).map( ([k,v]) => {return {[k]:v}} );
    };

    JSON.stringify(mapToAoO(map)); // '[{"foo":"bar"},{"baz":"quz"}]'


    Going to Array of Arrays



    This is even easier, you can just use



    JSON.stringify( Array.from(map) ); // '[["foo","bar"],["baz","quz"]]'





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29085197%2fhow-do-you-json-stringify-an-es6-map%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      37














      You can't.



      The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.




      My keys are guaranteed to be strings and my values will always be lists




      In this case, you can use a plain object. It will have these advantages:




      • It will be able to be stringified to JSON.

      • It will work on older browsers.

      • It might be faster.






      share|improve this answer





















      • 6





        for the curious-in the latest chrome, any map serializes into '{}'

        – Capaj
        Jan 6 '16 at 16:20











      • I have explained here what exactly I meant when I said "you can't".

        – Oriol
        Jan 29 '16 at 6:20






      • 5





        "It might be faster" - Do you have any source on that? I'm imagining a simple hash-map must be faster than a full blown object, but I have no proof. :)

        – Lilleman
        Feb 11 '16 at 18:01






      • 1





        @Xplouder That test uses expensive hasOwnProperty. Without that, Firefox iterates objects much faster than maps. Maps are still faster on Chrome, though. jsperf.com/es6-map-vs-object-properties/95

        – Oriol
        Mar 18 '16 at 13:49






      • 1





        True, seems that Firefox 45v iterates objects away faster than Chrome +49v. However Maps still wins vs objects in Chrome.

        – Xplouder
        Mar 18 '16 at 19:52
















      37














      You can't.



      The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.




      My keys are guaranteed to be strings and my values will always be lists




      In this case, you can use a plain object. It will have these advantages:




      • It will be able to be stringified to JSON.

      • It will work on older browsers.

      • It might be faster.






      share|improve this answer





















      • 6





        for the curious-in the latest chrome, any map serializes into '{}'

        – Capaj
        Jan 6 '16 at 16:20











      • I have explained here what exactly I meant when I said "you can't".

        – Oriol
        Jan 29 '16 at 6:20






      • 5





        "It might be faster" - Do you have any source on that? I'm imagining a simple hash-map must be faster than a full blown object, but I have no proof. :)

        – Lilleman
        Feb 11 '16 at 18:01






      • 1





        @Xplouder That test uses expensive hasOwnProperty. Without that, Firefox iterates objects much faster than maps. Maps are still faster on Chrome, though. jsperf.com/es6-map-vs-object-properties/95

        – Oriol
        Mar 18 '16 at 13:49






      • 1





        True, seems that Firefox 45v iterates objects away faster than Chrome +49v. However Maps still wins vs objects in Chrome.

        – Xplouder
        Mar 18 '16 at 19:52














      37












      37








      37







      You can't.



      The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.




      My keys are guaranteed to be strings and my values will always be lists




      In this case, you can use a plain object. It will have these advantages:




      • It will be able to be stringified to JSON.

      • It will work on older browsers.

      • It might be faster.






      share|improve this answer















      You can't.



      The keys of a map can be anything, including objects. But JSON syntax only allows strings as keys. So it's impossible in a general case.




      My keys are guaranteed to be strings and my values will always be lists




      In this case, you can use a plain object. It will have these advantages:




      • It will be able to be stringified to JSON.

      • It will work on older browsers.

      • It might be faster.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 16 '15 at 19:42









      rynop

      23k196768




      23k196768










      answered Mar 16 '15 at 19:34









      OriolOriol

      160k38266372




      160k38266372








      • 6





        for the curious-in the latest chrome, any map serializes into '{}'

        – Capaj
        Jan 6 '16 at 16:20











      • I have explained here what exactly I meant when I said "you can't".

        – Oriol
        Jan 29 '16 at 6:20






      • 5





        "It might be faster" - Do you have any source on that? I'm imagining a simple hash-map must be faster than a full blown object, but I have no proof. :)

        – Lilleman
        Feb 11 '16 at 18:01






      • 1





        @Xplouder That test uses expensive hasOwnProperty. Without that, Firefox iterates objects much faster than maps. Maps are still faster on Chrome, though. jsperf.com/es6-map-vs-object-properties/95

        – Oriol
        Mar 18 '16 at 13:49






      • 1





        True, seems that Firefox 45v iterates objects away faster than Chrome +49v. However Maps still wins vs objects in Chrome.

        – Xplouder
        Mar 18 '16 at 19:52














      • 6





        for the curious-in the latest chrome, any map serializes into '{}'

        – Capaj
        Jan 6 '16 at 16:20











      • I have explained here what exactly I meant when I said "you can't".

        – Oriol
        Jan 29 '16 at 6:20






      • 5





        "It might be faster" - Do you have any source on that? I'm imagining a simple hash-map must be faster than a full blown object, but I have no proof. :)

        – Lilleman
        Feb 11 '16 at 18:01






      • 1





        @Xplouder That test uses expensive hasOwnProperty. Without that, Firefox iterates objects much faster than maps. Maps are still faster on Chrome, though. jsperf.com/es6-map-vs-object-properties/95

        – Oriol
        Mar 18 '16 at 13:49






      • 1





        True, seems that Firefox 45v iterates objects away faster than Chrome +49v. However Maps still wins vs objects in Chrome.

        – Xplouder
        Mar 18 '16 at 19:52








      6




      6





      for the curious-in the latest chrome, any map serializes into '{}'

      – Capaj
      Jan 6 '16 at 16:20





      for the curious-in the latest chrome, any map serializes into '{}'

      – Capaj
      Jan 6 '16 at 16:20













      I have explained here what exactly I meant when I said "you can't".

      – Oriol
      Jan 29 '16 at 6:20





      I have explained here what exactly I meant when I said "you can't".

      – Oriol
      Jan 29 '16 at 6:20




      5




      5





      "It might be faster" - Do you have any source on that? I'm imagining a simple hash-map must be faster than a full blown object, but I have no proof. :)

      – Lilleman
      Feb 11 '16 at 18:01





      "It might be faster" - Do you have any source on that? I'm imagining a simple hash-map must be faster than a full blown object, but I have no proof. :)

      – Lilleman
      Feb 11 '16 at 18:01




      1




      1





      @Xplouder That test uses expensive hasOwnProperty. Without that, Firefox iterates objects much faster than maps. Maps are still faster on Chrome, though. jsperf.com/es6-map-vs-object-properties/95

      – Oriol
      Mar 18 '16 at 13:49





      @Xplouder That test uses expensive hasOwnProperty. Without that, Firefox iterates objects much faster than maps. Maps are still faster on Chrome, though. jsperf.com/es6-map-vs-object-properties/95

      – Oriol
      Mar 18 '16 at 13:49




      1




      1





      True, seems that Firefox 45v iterates objects away faster than Chrome +49v. However Maps still wins vs objects in Chrome.

      – Xplouder
      Mar 18 '16 at 19:52





      True, seems that Firefox 45v iterates objects away faster than Chrome +49v. However Maps still wins vs objects in Chrome.

      – Xplouder
      Mar 18 '16 at 19:52













      8














      You can't directly stringify the Map instance as it doesn't have any properties, but you can convert it to an array of tuples:



      jsonText = JSON.stringify(Array.from(map.entries()));


      For the reverse, use



      map = new Map(JSON.parse(jsonText));





      share|improve this answer




























        8














        You can't directly stringify the Map instance as it doesn't have any properties, but you can convert it to an array of tuples:



        jsonText = JSON.stringify(Array.from(map.entries()));


        For the reverse, use



        map = new Map(JSON.parse(jsonText));





        share|improve this answer


























          8












          8








          8







          You can't directly stringify the Map instance as it doesn't have any properties, but you can convert it to an array of tuples:



          jsonText = JSON.stringify(Array.from(map.entries()));


          For the reverse, use



          map = new Map(JSON.parse(jsonText));





          share|improve this answer













          You can't directly stringify the Map instance as it doesn't have any properties, but you can convert it to an array of tuples:



          jsonText = JSON.stringify(Array.from(map.entries()));


          For the reverse, use



          map = new Map(JSON.parse(jsonText));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 19:10









          BergiBergi

          375k60565897




          375k60565897























              1














              While there is no method provided by ecmascript yet, this can still be done using JSON.stingify if you map the Map to a JavaScript primitive. Here is the sample Map we'll use.



              const map = new Map();
              map.set('foo', 'bar');
              map.set('baz', 'quz');


              Going to an JavaScript Object



              You can convert to JavaScript Object literal with the following helper function.



              const mapToObj = m => {
              return Array.from(m).reduce((obj, [key, value]) => {
              obj[key] = value;
              return obj;
              }, {});
              };

              JSON.stringify(mapToObj(map)); // '{"foo":"bar","baz":"quz"}'


              Going to a JavaScript Array of Objects



              The helper function for this one would be even more compact



              const mapToAoO = m => {
              return Array.from(m).map( ([k,v]) => {return {[k]:v}} );
              };

              JSON.stringify(mapToAoO(map)); // '[{"foo":"bar"},{"baz":"quz"}]'


              Going to Array of Arrays



              This is even easier, you can just use



              JSON.stringify( Array.from(map) ); // '[["foo","bar"],["baz","quz"]]'





              share|improve this answer






























                1














                While there is no method provided by ecmascript yet, this can still be done using JSON.stingify if you map the Map to a JavaScript primitive. Here is the sample Map we'll use.



                const map = new Map();
                map.set('foo', 'bar');
                map.set('baz', 'quz');


                Going to an JavaScript Object



                You can convert to JavaScript Object literal with the following helper function.



                const mapToObj = m => {
                return Array.from(m).reduce((obj, [key, value]) => {
                obj[key] = value;
                return obj;
                }, {});
                };

                JSON.stringify(mapToObj(map)); // '{"foo":"bar","baz":"quz"}'


                Going to a JavaScript Array of Objects



                The helper function for this one would be even more compact



                const mapToAoO = m => {
                return Array.from(m).map( ([k,v]) => {return {[k]:v}} );
                };

                JSON.stringify(mapToAoO(map)); // '[{"foo":"bar"},{"baz":"quz"}]'


                Going to Array of Arrays



                This is even easier, you can just use



                JSON.stringify( Array.from(map) ); // '[["foo","bar"],["baz","quz"]]'





                share|improve this answer




























                  1












                  1








                  1







                  While there is no method provided by ecmascript yet, this can still be done using JSON.stingify if you map the Map to a JavaScript primitive. Here is the sample Map we'll use.



                  const map = new Map();
                  map.set('foo', 'bar');
                  map.set('baz', 'quz');


                  Going to an JavaScript Object



                  You can convert to JavaScript Object literal with the following helper function.



                  const mapToObj = m => {
                  return Array.from(m).reduce((obj, [key, value]) => {
                  obj[key] = value;
                  return obj;
                  }, {});
                  };

                  JSON.stringify(mapToObj(map)); // '{"foo":"bar","baz":"quz"}'


                  Going to a JavaScript Array of Objects



                  The helper function for this one would be even more compact



                  const mapToAoO = m => {
                  return Array.from(m).map( ([k,v]) => {return {[k]:v}} );
                  };

                  JSON.stringify(mapToAoO(map)); // '[{"foo":"bar"},{"baz":"quz"}]'


                  Going to Array of Arrays



                  This is even easier, you can just use



                  JSON.stringify( Array.from(map) ); // '[["foo","bar"],["baz","quz"]]'





                  share|improve this answer















                  While there is no method provided by ecmascript yet, this can still be done using JSON.stingify if you map the Map to a JavaScript primitive. Here is the sample Map we'll use.



                  const map = new Map();
                  map.set('foo', 'bar');
                  map.set('baz', 'quz');


                  Going to an JavaScript Object



                  You can convert to JavaScript Object literal with the following helper function.



                  const mapToObj = m => {
                  return Array.from(m).reduce((obj, [key, value]) => {
                  obj[key] = value;
                  return obj;
                  }, {});
                  };

                  JSON.stringify(mapToObj(map)); // '{"foo":"bar","baz":"quz"}'


                  Going to a JavaScript Array of Objects



                  The helper function for this one would be even more compact



                  const mapToAoO = m => {
                  return Array.from(m).map( ([k,v]) => {return {[k]:v}} );
                  };

                  JSON.stringify(mapToAoO(map)); // '[{"foo":"bar"},{"baz":"quz"}]'


                  Going to Array of Arrays



                  This is even easier, you can just use



                  JSON.stringify( Array.from(map) ); // '[["foo","bar"],["baz","quz"]]'






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 11 at 5:42

























                  answered Jan 11 at 5:27









                  Evan CarrollEvan Carroll

                  36.1k24146247




                  36.1k24146247






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29085197%2fhow-do-you-json-stringify-an-es6-map%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen