title: Coffeescript / Node.js - How to remove elements from an object based on comparison with DB
body: <p>I'm working on a project which will post periodic XML updates for users.  I'm attempting to suppress information which hasn't changed since the last update.</p>
  
  <p>Within node.js, I have assembled a user object which will be used to generate the XML:</p>
  
  <pre><code>users = 
    # actual application has 25 properties per user
    [
      {
        USERID: 150927,
        EMAIL: 'irving.block@email.net',
        FIRSTNAME: 'Irving',
        LASTNAME: 'Block',
        COLOR: 'Green'
      },
      {
        USERID: 1246007,
        EMAIL: 'allen.adler@email.net',
        FIRSTNAME: 'Adler',
        LASTNAME: 'Allen',
        COLOR: 'Blue'
      },
      {
        CLKEY: 1248350,
        EMAIL: 'walter.pidgeon@email.net',
        FIRSTNAME: 'Walter',
        LASTNAME: 'Pidgeon',
        COLOR: 'Red'
      }
    ]
  </code></pre>
  
  <p>I also have an object loaded which helps map user data to the eventual XML fields:</p>
  
  <pre><code>xml_map =
    [
      {
        ID: 1,
        ELEMENT: 'xml_map_assigned_id',
        MAPPPED: 'USERID' 
      },
      {
        ID: 2,
        ELEMENT: 'xml_map_email_address',
        MAPPPED: 'EMAIL' 
      },
      {
        ID: 3,
        ELEMENT: 'xml_map_user_firstname',
        MAPPPED: 'FIRSTNAME' 
      },
      {
        ID: 4,
        ELEMENT: 'xml_map_user_lastname',
        MAPPPED: 'LASTNAME' 
      },
      {
        ID: 5,
        ELEMENT: 'xml_map_user_color',
        MAPPED: 'COLOR'
      }
    ]
  </code></pre>
  
  <p>Using these objects, I need to compare with the last update, and remove matching properties (since I only want to post updates for data that has changed).  Here's an example of what the last update (stored in the DB) might look like:</p>
  
  <pre><code># nothing like this is used in the real application
  # just showing here to give an idea of what the DB could look like
  db_data = 
    [ 
      {
        USERID: 150927,
        EMAIL: 'irving.block@email.net',
        FIRSTNAME: 'IRVING',
        LASTNAME: 'Block',
        COLOR: 'Orange'
      },
      {
        USERID: 1246007,
        EMAIL: 'new.email@somewhere.com',
        FIRSTNAME: 'Adler',
        LASTNAME: 'Allen',
        COLOR: 'Blue'
      },
      {
        USERID: 1248350,
        EMAIL: 'walter.pidgeon@email.net',
        FIRSTNAME: 'Walt',
        LASTNAME: 'Pidgeon',
        COLOR: 'Red'
      }
    ]
  </code></pre>
  
  <p>Using these examples, I'd like to end up with a User object like this (since only 1 thing has changed for each user):</p>
  
  <pre><code>users_final = 
    [
      {
        USERID: 150927,
        COLOR: 'Orange'
      },
      {
        USERID: 1246007,
        EMAIL: 'new.email@somewhere.com',
      },
      {
        USERID: 1248350,
        FIRSTNAME: 'Walt',
      }
    ]
  </code></pre>
  
  <p>And now for the pseudo-code that shows how I've been trying to accomplish this:</p>
  
  <pre><code>async = require "async"
  
  handleChanges = (users, map, callback) -&gt;
    sql1 = "select..."
    sql2 = "update..."
    sql3 = "insert..."
    db.query sql1, [], (err, rows, def) -&gt;
      if err? then console.log err
      if rows[0]
        if rows[0].ELEMENT_VALUE.toString() is users[map.MAPPED].toString() 
        # nothing has changed since last update
          db.query sql2, ['Old'], (err, rows, def) -&gt;
            if err? then console.log err
            callback null, map.MAPPED
        else # this value has changed since the last update. Run update query
          db.query sql2, ['New'], (err, rows, def) -&gt;
            if err? then console.log err
            callback null, null
      else # no value has ever been saved for this combo. Run insert query
        db.query sql3, [], (err, rows, def) -&gt;
          if err? then console.log err
          callback null, null
  
  updateStore = (users, callback) -&gt;
    for map in xml_map
      do(map) -&gt;
        handleChanges users, map, (err, del) -&gt;
          if del? then delete r[del]
    callback null, r
  
  async.map users, updateStore, (err, results) -&gt;
    console.log results
  </code></pre>
  
  <p>This obviously doesn't work right.  I'm sure the updateStore function needs to be doing something different, but I'm at a loss.</p>
  
  <p>Thanks in advance for your help!</p>