title: Converting JSON array from one format to another using JavaScript
body: <p>I'm a newbie here and would like some guidance. How do I convert the JSON array (input) to another JSON array but grouping by id and need just the values of 'id' and 'sid'.</p>
  <p>I have tried using lodash, array.reduce but couldn't get the expected output.</p>
  <p>The actual input will be a large JSON array and any suggestions on how to solve this efficiently will be much appreciated.</p>
  <p><strong>Input:</strong></p>
  <pre class="lang-js prettyprint-override"><code>[
      {
          &quot;id&quot;: &quot;11111&quot;,
          &quot;sid&quot;: &quot;12345&quot;
      },
      {
          &quot;id&quot;: &quot;22222&quot;,
          &quot;sid&quot;: &quot;23456&quot;
      },
      {
          &quot;id&quot;: &quot;22222&quot;,
          &quot;sid&quot;: &quot;34567&quot;
      }
  ]
  </code></pre>
  <p><strong>Expected Output:</strong></p>
  <pre class="lang-js prettyprint-override"><code>[
      {
          &quot;11111&quot;: [
              &quot;12345&quot;
          ]
      },
      {
          &quot;22222&quot;: [
              &quot;23456&quot;,
              &quot;34567&quot;
          ]
      }
  ]
  </code></pre>
  <p><strong>lodash:</strong></p>
  <pre class="lang-js prettyprint-override"><code>_.groupBy(array, x =&gt; x.id);
  </code></pre>
  <p><strong>lodash output:</strong></p>
  <pre class="lang-js prettyprint-override"><code>{
    '11111': [
      { id: '11111', sid: '12345' }
    ],
    '22222': [
      { id: '22222', sid: '23456' },
      { id: '22222', sid: '34567' } 
    ]
  }
  </code></pre>
  <p><strong>array.reduce:</strong></p>
  <pre class="lang-js prettyprint-override"><code>const groupById = (array, key, value) =&gt; {
    return array.reduce((result, currentValue) =&gt; {
      (result[currentValue[key]] = result[currentValue[key]] || []).push(
        currentValue[value]
      );
      return result;
    }, {});
  };
  </code></pre>
  <p><strong>array.reduce output:</strong></p>
  <pre class="lang-js prettyprint-override"><code>{
      &quot;11111&quot;: [
          &quot;12345&quot;
      ],
      &quot;22222&quot;: [
          &quot;23456&quot;,
          &quot;34567&quot;
      ]
  }
  </code></pre>