title: How to remove original array elements after mutating changes before writing update
body: <p>I have a <code>source.json</code> that contains playlist entries like so:</p>
  <pre><code>&quot;playlists&quot; : [
        {
          &quot;id&quot; : &quot;1&quot;,
          &quot;owner_id&quot; : &quot;2&quot;,
          &quot;song_ids&quot; : [
            &quot;8&quot;,
            &quot;32&quot;
          ]
        },
        {
          &quot;id&quot; : &quot;2&quot;,
          &quot;owner_id&quot; : &quot;3&quot;,
          &quot;song_ids&quot; : [
            &quot;6&quot;,
            &quot;8&quot;,
            &quot;11&quot;
          ]
        },
        {
          &quot;id&quot; : &quot;3&quot;,
          &quot;owner_id&quot; : &quot;7&quot;,
          &quot;song_ids&quot; : [
            &quot;7&quot;,
            &quot;12&quot;,
            &quot;13&quot;,
            &quot;16&quot;,
            &quot;2&quot;
          ]
        }
      ]
  </code></pre>
  <p>and then a <code>changes.json</code> file that has updates I want to make to the <code>source.json</code> file:</p>
  <pre><code>{
      &quot;playlists&quot;: [{
              &quot;action&quot;: &quot;update&quot;,
              &quot;payload&quot;: [{
                  &quot;id&quot;: &quot;1&quot;,
                  &quot;owner_id&quot;: &quot;2&quot;,
                  &quot;song_ids&quot;: [
                      &quot;1&quot;
                  ]
              }]
          },
          {
              &quot;action&quot;: &quot;create&quot;,
              &quot;payload&quot;: [{
                  &quot;id&quot;: &quot;1&quot;,
                  &quot;owner_id&quot;: &quot;2&quot;,
                  &quot;song_ids&quot;: [
                      &quot;8&quot;,
                      &quot;32&quot;
                  ]
              }]
          },
          {
              &quot;action&quot;: &quot;delete&quot;,
              &quot;payload&quot;: [{
                  &quot;id&quot;: &quot;1&quot;
              }]
          }
      ]
  }
  </code></pre>
  <p>It's a command line program, that in the json contains directives on how to process, i.e. <code>&quot;action&quot;:&quot;update&quot;</code> and then the <code>payload</code> to update with.  After mutating the array, I wish to write out the updated result to <code>output.json</code>. I have the code working pretty good, but there is one problem. When it updates it leaves the old entries in the original array. How do you remove those original entries before updating?</p>
  <p>Here is my code:</p>
  <pre><code>    // Stream in source file
      const data = JSON.parse(fs.readFileSync('./' + inputFile));
  
      // Stream in changes file
      const changes = JSON.parse(fs.readFileSync('./' + changesFile));
  
      for(const [table, actions] of Object.entries(changes)) {
          if(!(table in data))continue;
  
          // iterate through inner CRUD directives and mutate payloads
          for(const actionDetail of actions) {
              if(actionDetail.action === 'create') {
                  console.log('This is a CREATE entry');
                  data[table].push(...actionDetail.payload);
              }
  
              if(actionDetail.action === 'delete') {
                  console.log('This is a DELETE entry');
              }
  
              if(actionDetail.action === 'update') {
                  console.log('This is a UPDATE entry');
                  
                  // const playlistUpdate = get id's from changes.json to update;
                  const result = data.playlists.filter(playlist =&gt; playlist.id !== playlistUpdate);
  
                  // grr! writes mutation but leaves old entries in original source.json playlist array of objects. grr.
                  data[table].push(result);
              }
  
          }
      }
      fs.writeFileSync('output.json', JSON.stringify(data, null, 2));
  </code></pre>
  <p>The current <code>output.json</code> It doesn't update the <code>id: 1</code> with new payload, it concatenates. grr.</p>
  <pre><code>&quot;playlists&quot;: [
      {
        &quot;id&quot;: &quot;1&quot;,
        &quot;owner_id&quot;: &quot;2&quot;,
        &quot;song_ids&quot;: [
          &quot;8&quot;,
          &quot;32&quot;
        ]
      },
      {
        &quot;id&quot;: &quot;2&quot;,
        &quot;owner_id&quot;: &quot;3&quot;,
        &quot;song_ids&quot;: [
          &quot;6&quot;,
          &quot;8&quot;,
          &quot;11&quot;
        ]
      },
      {
        &quot;id&quot;: &quot;3&quot;,
        &quot;owner_id&quot;: &quot;7&quot;,
        &quot;song_ids&quot;: [
          &quot;7&quot;,
          &quot;12&quot;,
          &quot;13&quot;,
          &quot;16&quot;,
          &quot;2&quot;
        ]
      },
      {
        &quot;id&quot;: &quot;1&quot;,
        &quot;owner_id&quot;: &quot;2&quot;,
        &quot;song_ids&quot;: [
          &quot;1&quot;
        ]
      }
    ]
  </code></pre>