title: Store first matched element in array for each in other array
body: <p>I have an array of objects which contain addresses. I need to perform the following:</p>
  
  <ul>
  <li>Extract each postcode from address values.</li>
  <li>Find unique postcodes and store amount of duplicates.</li>
  <li>Store the first address for each unique postcode in a new array along with the number of duplicates.</li>
  </ul>
  
  <p>Here is my current code. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const json = [
    { "id": "10093729341", "address": "1 Alpha Road CF14 6AA" },
    { "id": "10024750520", "address": "2 Alpha Road CF14 6AA" },
    { "id": "10025738368", "address": "3 Alpha Road CF14 6AF" },
    { "id": "10025738368", "address": "4 Alpha Road CF14 6AF" },
    { "id": "10025738368", "address": "4 Alpha Road CF14 6AB" }
  ]
  
  let allPostcodes = [];
  
  json.forEach(address =&gt; {
    const fullAddresses = address.address;
    const postcodes = fullAddresses.split(",").map(s =&gt; s.trim().match(/([A-Za-z]{1,2}\d{1,2})(\s?(\d?\w{2}))?/)).filter(e =&gt; e)[0][0]
    allPostcodes.push(postcodes);
  });
  
  const uniquePostcodes = [...new Set(allPostcodes)];
  
  uniquePostcodes.forEach(postcode =&gt; {
    const addresses = json.find(address =&gt; address.address.indexOf(postcode));
    console.log(addresses.address);
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>The final part above stops at the first match but for the first postcode only. I thought that in a loop it would work but it doesn't.</p>
  
  <p>My desired outcome would be this:</p>
  
  <pre><code>const array = [
    {
      "address": "1 Alpha Road CF14 6AA",
      "count": 2,
      "postcode": "CF14 6AA"
    },
    {
      "address": "3 Alpha Road CF14 6AF",
      "count": 2,
      "postcode": "CF14 6AF"
  
    },
    {
      "address": "4 Alpha Road CF14 6AB",
      "count": 1,
      "postcode": "CF14 6AB"
  
    }
  ]
  </code></pre>