title: How do I check to see if an object or its children/sub-objects has a property/key in Javascript?
body: <p>What I am looking for is basically a hasOwnProperty() or 'in' function which is recursive and looks down the chain to see if a key or property exists in an object.</p>
  
  <p>For example I have the object:</p>
  
  <pre><code>var user = {
    username: "Derpette",
    profile: {
      role: 'admin',
      sex: 'female',
      emails:['derpette42@derpmail.com']
      }
  }
  </code></pre>
  
  <p>I want to know if there is a key/property 'role' anywhere in the object or its sub-objects.
  So I want to be able to call something like <code>user.hasProperty('role')</code> and it would return <code>true</code>.</p>
  
  <p>Right now I am using if statements with a chain of assertions to check but it doesn't seem clean, especially for deep nested properties.</p>
  
  <p>For example I would do:</p>
  
  <pre><code>if(user &amp;&amp; user.profile &amp;&amp; user.profile.role){
      //Manipulate/Use user.profile.role here
  }
  </code></pre>
  
  <p>Another example of a use-case for this is if I am filtering mongoDB update modifiers coming from a client. I want to make sure they aren't updating their own role so I need to check that the property 'role' isn't somewhere in their mongoDB modifier json object. But it could be in a few places in a modifier so doing the if property chaining doesn't work well for this case.</p>
  
  <p>Do I need to write my own recursive hasOwnProperty? I am thinking that something like this should already exist somewhere in a project like jquery or underscores.js or sugar.js but I can't seem to find it.</p>