title: Merging elements in an array in a subarray with same key
body: <p>I am really confused why this approach is not working. I have two arrays, so I am iterating over the main array and then comparing the keys to copy the corresponding record in the array. This is the data example:</p>
  <pre><code>let r2 = [
      {
          &quot;record_guid&quot;: &quot;cb7fba9b-e8fc-40c8-9a2c-9ba2e06d1392&quot;,
          &quot;comment&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,&quot; 
      },
      {
          &quot;record_guid&quot;: &quot;9f0689e7-1fff-4731-9e60-35c80b62fd33&quot;,
          &quot;comment&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,&quot; 
      },
      {
          &quot;record_guid&quot;: &quot;b61c72a8-5c83-485e-8366-6f4a56d28b55&quot;,
          &quot;comment&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,&quot; 
      }
  ];
  
  let r1= [
    {
      &quot;record_guid&quot;: &quot;5e3dc291-662d-4ca0-895b-6272d51156fb&quot;,
      &quot;record_id&quot;: 1,
      &quot;parent_record_id&quot;: null,
      &quot;description&quot;: &quot;Introduction&quot;,
      &quot;sub_records&quot;: [
          {
              &quot;record_guid&quot;: &quot;cb7fba9b-e8fc-40c8-9a2c-9ba2e06d1392&quot;,
              &quot;record_id&quot;: 10,
              &quot;parent_record_id&quot;: 1,
              &quot;description&quot;: &quot;Background&quot;,
              &quot;sub_records&quot;: [],
          },
          {
              &quot;record_guid&quot;: &quot;9f0689e7-1fff-4731-9e60-35c80b62fd33&quot;,
              &quot;record_id&quot;: 11,
              &quot;parent_record_id&quot;: 1,
              &quot;description&quot;: &quot;Details&quot;,
              &quot;sub_records&quot;: [],
          },
          {
              &quot;record_guid&quot;: &quot;b61c72a8-5c83-485e-8366-6f4a56d28b55&quot;,
              &quot;record_id&quot;: 12,
              &quot;parent_record_id&quot;: 1,
              &quot;description&quot;: &quot;Regulations&quot;,
              &quot;sub_records&quot;: [],
          },
          
              ],
          }
      ],
  }
  ];
  </code></pre>
  <p>Something like this should work:</p>
  <pre><code>const mergeById = (r1, r2) =&gt;
      r1.map(itm =&gt; ({
          ...r2.find((item) =&gt; (item.record_guid === itm.record_guid) &amp;&amp; item),
          ...itm
      }));
  
  console.log(mergeById(r1, r2));
  </code></pre>
  <p>Or this other similar version:</p>
  <pre><code>let merged = [];
  
  for(let i=0; i&lt;r1.length; i++) {
    merged.push({
     ...r1[i], 
     ...(r2.find((itmInner) =&gt; itmInner.record_guid === r1[i].record_guid))}
    );
  }
   
  console.log(merged);
  </code></pre>
  <p>I am just not getting the comments field in the corresponding array element. Do you know why this is not working to copy the second array into the first array to get something like the following?</p>
  <pre><code>[
    {
      &quot;record_guid&quot;: &quot;5e3dc291-662d-4ca0-895b-6272d51156fb&quot;,
      &quot;record_id&quot;: 1,
      &quot;parent_record_id&quot;: null,
      &quot;description&quot;: &quot;Introduction&quot;,
      &quot;sub_records&quot;: [
          {
              &quot;record_guid&quot;: &quot;cb7fba9b-e8fc-40c8-9a2c-9ba2e06d1392&quot;,
              &quot;record_id&quot;: 10,
              &quot;parent_record_id&quot;: 1,
              &quot;description&quot;: &quot;Background&quot;,
              &quot;comment&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,&quot;
              &quot;sub_records&quot;: [],
          },
          {
              &quot;record_guid&quot;: &quot;9f0689e7-1fff-4731-9e60-35c80b62fd33&quot;,
              &quot;record_id&quot;: 11,
              &quot;parent_record_id&quot;: 1,
              &quot;description&quot;: &quot;Details&quot;,
              &quot;comment&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,&quot;
              &quot;sub_records&quot;: [],
          },
          {
              &quot;record_guid&quot;: &quot;b61c72a8-5c83-485e-8366-6f4a56d28b55&quot;,
              &quot;record_id&quot;: 12,
              &quot;parent_record_id&quot;: 1,
              &quot;description&quot;: &quot;Regulations&quot;,
              &quot;comment&quot;: &quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit,&quot;
              &quot;sub_records&quot;: [],
          },
          
              ],
          }
      ],
  }
  ]
  </code></pre>