title: JavaScript: check if duplicate key values exist in array of objects and remove all but most recently added object having that key value
body: <p>I'm trying to figure out how to check for duplicate values of keys between objects in the same array, then only keep the most recently added object having that value.</p>
  <p>For example, I have an array of objects I'm trying to filter duplicates of <code>m_id</code> out of, like so:</p>
  <pre class="lang-js prettyprint-override"><code>arr = [
    {id: &quot;id-1&quot;, m_id: &quot;1&quot;, color: &quot;blue&quot;},
    {id: &quot;id-2&quot;, m_id: &quot;1&quot;, color: &quot;green&quot;},
    {id: &quot;id-3&quot;, m_id: &quot;2&quot;, color: &quot;red&quot;},
    {id: &quot;id-4&quot;, m_id: &quot;2&quot;, color: &quot;yellow&quot;},
    {id: &quot;id-5&quot;, m_id: &quot;5&quot;, color: &quot;purple&quot;}
  ]
  </code></pre>
  <p>The desired result from the above example array of objects would be:</p>
  <pre class="lang-js prettyprint-override"><code>arr = [
    {id: &quot;id-2&quot;, m_id: &quot;1&quot;, color: &quot;green&quot;},
    {id: &quot;id-4&quot;, m_id: &quot;2&quot;, color: &quot;yellow&quot;},
    {id: &quot;id-5&quot;, m_id: &quot;5&quot;, color: &quot;purple&quot;}
  ]
  </code></pre>
  <p>As you can see, I have a simple <code>id</code> string value that gets incremented for the collection by design. What I'm trying to do is find if a duplicate <code>m_id</code> value exists between any two objects in the array, then remove the object(s) having the <em>lower</em> of the two ids. By lower, in this case, I mean non-numeric boolean comparison of the string ids (e.g., <code>Boolean(&quot;id-1&quot; &lt; &quot;id-2&quot;)</code> returns <code>true</code>).</p>
  <p>I've tried using <code>Array.prototype.filter()</code> in combination with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some" rel="nofollow noreferrer"><code>Array.prototype.some()</code></a> in numerous floundering permutations, but I keep running into trouble when I'm looking to compare key values of object elements in the <em>same</em> array inside my <code>filter</code> when the overall collection is not in scope. I'm at a loss so far in getting the results I need.</p>