title: Recursively strip specific properties from object JavaScript
body: <p>I am currently trying to write a function that checks the properties of an object and if that property has a key 'data', apply the value of that to its parent, unless the current element is already at the object root. So, given the object:</p>
  
  <pre><code>{
          data: {
            user: {
              data: {
                firstName: 'joe',
                data: {surname: 'bloggs'}
              }
            },
            message: 'this is a message'
          }
        }
  </code></pre>
  
  <p>It should return:</p>
  
  <pre><code>{
          data: {
            user: {
              firstName: 'joe',
              surname: 'bloggs'
            },
            message: 'this is a message'
          }
        }
  </code></pre>
  
  <p>So far, my logic looks like this:</p>
  
  <pre><code>function stripData(data, parent) {
      'use strict';
  
      return Object.assign({}, ...Object.keys(data).map(key =&gt; {
        if(key !== 'data' &amp;&amp; typeof data[key] === 'object' &amp;&amp; !parent){
          return ({[key]: exports.stripData(data[key], key)});
        }
        if(key === 'data' &amp;&amp; parent){
          Logger.verbose(`key is data and parent is set`);
          if(typeof data[key] === 'object'){
            return ({[parent]: exports.stripData(data[key], parent)});;
          }
          return ({[parent]: data[key]});
        }
        if(key === 'data'&amp;&amp;!parent ){
          return ({[key]: exports.stripData(data[key], key)});
        }
        return ({[key]: data[key]})
      }));
  }
  </code></pre>
  
  <p>This function so far just returns the same object that it is passed. Log statements tell me that it gets as far as the <code>message</code> key of the first object, but it has not recursively traversed the object tree by that time.</p>
  
  <p>There is probably an easier way of doing this, if possible I'd like to remain dependency free, although if someone can point me to a straightforward way of doing this with lodash, I'll use that.</p>