title: forEach skips an element when a previous element is deleted
body: <p>I have two arrays, <code>available_items</code> and <code>requested_items</code>. I want to remove elements from <code>requested_items</code> that are missing in <code>available_items</code>. Using <code>forEach</code> does not give the expected result apparently because the internal index increments even when an element is deleted and the next element would have the old index.</p>
  
  <p>Here's a test case (also in <a href="http://jsbin.com/ocozum/1/edit" rel="noreferrer">this jsbin</a>):</p>
  
  <pre><code>var available_items = [2, 5, 9, 36, 48, 23];
  var requested_items = [5, 12, 49, 30, 90, 17];
  requested_items.forEach(function(v, i, a) {
    if(available_items.indexOf(v) == -1) {
      console.log("will remove " + i + ' ' + v);
      a.splice(i, 1);
    } else console.log("will keep " + i + ' ' + v);
  });
  console.log('Resulting request array is ' + requested_items.toString());
  </code></pre>
  
  <p>The result is:</p>
  
  <pre><code>"will keep 0 5"
  "will remove 1 12"
  "will remove 2 30"
  "will remove 3 17"
  "Resulting request array is 5,49,90"
  </code></pre>
  
  <p>This will be repeated tens of thousands times, so, using libraries (e.g. underscore), is something I want to avoid if they adversely impact performance. </p>
  
  <p>So, my question is, what is the least expensive way to correct this?</p>