title: Array.prototype.splice() not removing Objects while iterating
body: <p>I am working with an array of <code>mongodb</code>'s <code>ObjectID</code> objects. I must check if this array contains duplicates, and if so, remove them.</p>
  
  <p>Here is the <code>removeDuplicate</code> function I have, which simply loops over an array with two indexes. If two objects are identical, then the <code>splice()</code> function will remove one of them.</p>
  
  <pre><code>function removeDuplicates(array) {
      var a = array.concat(); // Copy object
      for(var i=0; i&lt;a.length; ++i) {
        for(var j=i+1; j&lt;a.length; ++j) {
          console.log(a[i] + " vs " + a[j]);
            if(a[i].equals(a[j]))
              console.log("removed : " + a.splice(j--, 1));
        }
      }
      return a;
  }
  </code></pre>
  
  <p>Let's say I have a simple array with two identical <code>ObjectID</code> objects in it.
  Then I call the <code>removeDuplicates</code> function, passing the array.
  Finally, I print out the array itself.</p>
  
  <pre><code>var array = [];
  
  array.push(new ObjectID("56fc227026aed8e74a699b20"));
  array.push(new ObjectID("56fc227026aed8e74a699b20"));
  
  removeDuplicates(array);
  
  console.log(array);
  </code></pre>
  
  <p>Here is the output. As you can see, nothing is removed at the end.</p>
  
  <pre><code>56fc227026aed8e74a699b20 vs 56fc227026aed8e74a699b20
  removed : 56fc227026aed8e74a699b20
  [ 56fc227026aed8e74a699b20, 56fc227026aed8e74a699b20 ]
  </code></pre>
  
  <p>What am I doing wrong ?</p>