title: JavaScript Convert nested Map to object
body: <p>I want to convert an es6 nested Map to an object.</p>
  
  <p>I tried this code:</p>
  
  <pre><code>    mapToObjectRec(m) {
        let lo = {}
        for(let[k,v] of m) {
            if(v instanceof Map) {
                lo[k] = this.mapToObjectRec(v)
            }
            else {
                lo[k] = v
            }
        }
        return lo
      }
  </code></pre>
  
  <p>I want to do the opposite of this function:</p>
  
  <pre><code>export declare type IElementsMap = Map&lt;string, IElements&gt;;
  
  
  deserializeElements(json: any): IElementsMaps | any {
      const elementsMap = new Map&lt;string, IElementsMap&gt;();
  
      if (Array.isArray(json)) {
        json.forEach(outer =&gt; {
          const elementMap = new Map&lt;string, IElements&gt;();
          outer[1].forEach(inner =&gt; {
            elementMap.set(inner[0], inner[1]);
          });
          elementsMap.set(outer[0], elementMap);
        });
  
        return elementsMap;
      }
  
      return json;
    }
  </code></pre>
  
  <p>I want to send converted data inside payload request (Post request).</p>