Removing every instance of a key from Javascript Object
<p>Can someone explain what on earth I'm doing wrong here:</p>

<p>I have the following code:</p>

<pre><code>    console.log(table);
$.each(datatableFields, function (a, b) {
    if ($.inArray(b.substring(0, 3), neededFields) == -1) {
        console.log('deleting ' + b);
        delete table[b];
    }
});
console.log(table);
</code></pre>

<p>What I'm trying to achieve should be simple:</p>

<p>I have a Javascript Object which I want to 'strip down' and remove any unnecessary fields.</p>

<p>The 'neededFields' array simple contains a list of fields that I wish to keep, anything not in that array is removed from the master Object.</p>

<p>Running the code above, I can confirm the logic works, and sure enough Firebug shows me "deleting ###" perfectly for any fields in the master Object and NOT in the 'neededFields'.</p>

<p>BUT...</p>

<p>It doesn't appear as if my 'delete table[b]' command works as intended as both 'console.log(table)' return the same results!</p>

<p>Any thoughts?</p>

<p>As an example if I have the following Object:</p>

<blockquote>
  <p>{ABC:123, CDE:456, FGH:789, IJK:012, LMN:345},</p>
  
  <p>{ABC:123, CDE:456, FGH:789, IJK:012, LMN:345},</p>
  
  <p>{ABC:123, CDE:456, FGH:789, IJK:012, LMN:345},</p>
  
  <p>{ABC:123, CDE:456, FGH:789, IJK:012, LMN:345},</p>
  
  <p>{ABC:123, CDE:456, FGH:789, IJK:012, LMN:345},</p>
  
  <p>{ABC:123, CDE:456, FGH:789, IJK:012, LMN:345},</p>
  
  <p>{ABC:123, CDE:456, FGH:789, IJK:012, LMN:345}
  (etc......)</p>
</blockquote>

<p>And removing certain keys (say IJK and ABC), my end result would be:</p>

<blockquote>
  <p>{CDE:456, FGH:789, LMN:345},</p>
  
  <p>{CDE:456, FGH:789, LMN:345},</p>
  
  <p>{CDE:456, FGH:789, LMN:345},</p>
  
  <p>{CDE:456, FGH:789, LMN:345},</p>
  
  <p>{CDE:456, FGH:789, LMN:345},</p>
  
  <p>{CDE:456, FGH:789, LMN:345},</p>
  
  <p>{CDE:456, FGH:789, LMN:345}
  (etc......)</p>
</blockquote>

<p><em>edit</em></p>

<p>"datatableFields" is an array that contains all the keys within the master object, likewise 'neededFields' is an array that contains keys which need to be kept.</p>
