title: How to remove all nested arrays and objects from javascript object
body: <p>I have an angularjs scope object with various nested objects and arrays like so:</p>
  
  <pre><code>{
    prop1: 'prop1'
    prop2: 'prop2'
    prop3: 'prop3'
    nestedArray1:
      0:
        prop1: 'prop1'
        prop2: 'prop2'
      1:
        prop1: 'prop1'
        prop2: 'prop2'
    nestedObj1:
      prop1: 'prop1'
      prop2: 'prop2'
    nestedObj2:
      prop1: 'prop1'
      prop2: 'prop2'
    ....
  }
  </code></pre>
  
  <p>I need to save the top level object via an api, and am currently using the following coffeescript function to manually delete the nested objects and arrays before sending it to the server:</p>
  
  <pre><code>$scope.save = -&gt;
      params = angular.copy($scope.mainObj)
      delete params.nestedArray1
      delete params.nestedObj2
      delete params.nestedObj3
      $api.update(obj: params).$promise.then ((response) -&gt;
          Flash.create('success', 'Changes saved.')
          ), (error) -&gt;
              Flash.create('danger', 'There\'s been a problem with our servers. Please try again later.')
  </code></pre>
  
  <p>How can I refactor the function to recursively remove all nested objects and arrays, so I can reuse it elsewhere?</p>