title: Create nested json from flattened JSON in javascript
body: <p>Started with javascript recently. I have a flattened json which looks like :</p>
  
  <pre><code>[
  {"timestamp":"2019-01-01T00:00:00.000Z","MarketShare":89905.0,"Territory":"PHILADELPHIA PA"},
  {"timestamp":"2019-01-01T00:00:00.000Z","MarketShare":82991.0,"Territory":"SEATTLE WA"},
  {"timestamp":"2019-02-01T00:00:00.000Z","MarketShare":73662.0,"Territory":"PHILADELPHIA PA"},
  {"timestamp":"2019-02-01T00:00:00.000Z","MarketShare":67059.0,"Territory":"SEATTLE WA"},
  {"timestamp":"2019-03-01T00:00:00.000Z","MarketShare":72898.0,"Territory":"PHILADELPHIA PA"},
  {"timestamp":"2019-03-01T00:00:00.000Z","MarketShare":68013.0,"Territory":"SEATTLE WA"},
  {"timestamp":"2019-04-01T00:00:00.000Z","MarketShare":74607.0,"Territory":"PHILADELPHIA PA"},
  {"timestamp":"2019-04-01T00:00:00.000Z","MarketShare":66769.0,"Territory":"SEATTLE WA"},
  {"timestamp":"2019-05-01T00:00:00.000Z","MarketShare":93932.0,"Territory":"PHILADELPHIA PA"},
  {"timestamp":"2019-05-01T00:00:00.000Z","MarketShare":86514.0,"Territory":"SEATTLE WA"}
  ]
  </code></pre>
  
  <p>Basically, for a given timestamp, I want to collect MarketShare and Territory under the result key.<br>
  The expected output is :</p>
  
  <pre><code>    [
     {
        "timestamp":"2019-01-01T00:00:00.000Z",
        "result":[
           {
              "MarketShare":89905.0,
              "Territory":"PHILADELPHIA PA"
           },
           {
              "MarketShare":82991.0,
              "Territory":"SEATTLE WA"
           },
  
        ]
     },
     {
        "timestamp":"2019-02-01T00:00:00.000Z",
        "result":[
           {
              "MarketShare":73662.0,
              "Territory":"PHILADELPHIA PA"
           },
           {
              "MarketShare":67059.0,
              "Territory":"SEATTLE WA"
           }
        ]
     },
     {
        "timestamp":"2019-03-01T00:00:00.000Z",
        "result":[
           {
              "MarketShare":72898.0,
              "Territory":"PHILADELPHIA PA"
           },
           {
              "MarketShare":68013.0,
              "Territory":"SEATTLE WA"
           }
        ]
     },
     {
        "timestamp":"2019-04-01T00:00:00.000Z",
        "result":[
           {
              "MarketShare":74607.0,
              "Territory":"PHILADELPHIA PA"
           },
           {
              "MarketShare":66769.0,
              "Territory":"SEATTLE WA"
           }
        ]
     },
     {
        "timestamp":"2019-05-01T00:00:00.000Z",
        "result":[
           {
              "MarketShare":93932.0,
              "Territory":"PHILADELPHIA PA"
           },
           {
              "MarketShare":86514.0,
              "Territory":"SEATTLE WA"
           }
        ]
     }
  ]
  </code></pre>
  
  <p>I am trying to create a generic utility that puts any number of key value pair under a given timestamp. The progress made so far:</p>
  
  <pre><code>topNFormat=[]
  for(var i = 0; i &lt; firstResult.length; i++) {
      //res = [firstResult[i]["Territory"]
      topNFormat[timestamp] = firstResult[i][timestamp] || {};
      topNFormat[result] = firstResult[i][result] || [];
      topNFormat[result].push(firstResult[i]);
      //return collection;
  }
  </code></pre>