title: convert multi-value Map<String, Object [ ]> to json in javascript
body: <p>I have a <code>Map&lt;String, List&lt;Object&gt;&gt;</code> and i want to convert it to <code>JSON</code>.</p>
  
  <p>Here is what I have: <br>
  <strong>Point 1:</strong> Declaring variables</p>
  
  <pre><code>var map = new Map(), //map
  jsonObject= {}, // oject that needs to be converted to JSON
  value1 = { topic: "someText1", status: "0", action: "1", comment: "someComment1" }, // values of Map
  value2 = { topic: "someText2", status: "0", action: "0", comment: "someComment2" },
  value3 = { topic: "someText3", status: "1", action: "1", comment: "someComment3" };
  </code></pre>
  
  <p><strong>Point 2:</strong> function to populate multi-value map</p>
  
  <pre><code>function populateMap(map, k, v) {
      map[k] = map[k] || [];
      map[k].push(v);
  }
  </code></pre>
  
  <p><strong>Point 3:</strong> populating Map <br>
  <em>// key of map is concatenation of various attributes from web page, separated by <strong>|</em></strong></p>
  
  <pre><code>populateMap(map, 'release|attachment|license1', value1);
  populateMap(map, 'release|attachment|license1', value2);
  populateMap(map, 'release1|attachment1|license1', value1);
  populateMap(map, 'release1|attachment1|license2', value2);
  populateMap(map, 'release1|attachment1|license3', value3);
  populateMap(map, 'release2|attachment2|license2', value1);
  populateMap(map, 'release2|attachment2|license2', value2);
  </code></pre>
  
  <p><strong>Point 4:</strong> iterating map and populating jsonObject </p>
  
  <pre><code>for (var i in map) {
      var keys = i.split('|'), // splitting keys based on |
      last = keys.pop(),
      values = map[i];
      values.forEach(function (item, index) {
          keys.reduce((r, a) =&gt; r[a] = r[a] || {}, jsonObject)[last] = item;
      });
  }
  </code></pre>
  
  <p><strong>Point 5:</strong> current output (jsonObject printed on console)</p>
  
  <pre><code>{
    "release": {
      "attachment": {
        "license1": "[Object]"
      }
    },
    "release1": {
      "attachment1": {
        "license1": "[Object]",
        "license2": "[Object]",
        "license3": "[Object]"
      }
    },
    "release2": {
      "attachment2": {
        "license2": "[Object]"
      }
    }
  }
  </code></pre>
  
  <p><strong>Point 6:</strong> expected output (jsonObject)</p>
  
  <pre><code>{
    "release": {
      "attachment": {
        "license1": "[Object, Object]" // expecting array of objects here ^^
      }
    },
    "release1": {
      "attachment1": {
        "license1": "[Object]",
        "license2": "[Object]",
        "license3": "[Object]"
      }
    },
    "release2": {
      "attachment2": {
        "license2": "[Object, Object]"
      }
    }
  }
  </code></pre>
  
  <p>^^ Since map is having array of objects for that key, So i want to have array of objects in <code>jsonObject</code>.</p>
  
  <p>Can someone help me with tweak needs to be done in <code>map</code> traversal in <strong>Point 4</strong> to obtain expected result?</p>