title: Merge JavaScript objects in array with same key
body: <p>What is the best way to merge array contents from JavaScript objects sharing a key in common?</p>
  <p>How can <code>array</code> in the example below be reorganized into <code>output</code>? Here, all <code>value</code> keys (<em>whether an array or not</em>) are merged into all objects sharing the same <code>name</code> key.</p>
  <pre><code>var array = [
      {
          name: &quot;foo1&quot;,
          value: &quot;val1&quot;
      }, {
          name: &quot;foo1&quot;,
          value: [
              &quot;val2&quot;,
              &quot;val3&quot;
          ]
      }, {
          name: &quot;foo2&quot;,
          value: &quot;val4&quot;
      }
  ];
  
  var output = [
      {
          name: &quot;foo1&quot;,
          value: [
              &quot;val1&quot;,
              &quot;val2&quot;,
              &quot;val3&quot;
          ]
      }, {
          name: &quot;foo2&quot;,
          value: [
              &quot;val4&quot;
          ]
      }
  ];
  </code></pre>