title: Recursive AngularJS filter
body: <p>I have a tree structure <a href="https://gist.github.com/Almaron/03e9bdeda4edc82fdb6d" rel="nofollow">like this</a> stored in the <code>$scope.tree</code> object. I want to create a filter, that I could apply to this tree to shrink it to <a href="https://gist.github.com/Almaron/8741e5a7d018263bc5f1" rel="nofollow">this</a> to be able to do smth like this <code>$scope.cut_tree = $filter('cutTree')($scope.tree)</code> and get that minified output.</p>
  
  <p>But I have really no idea how to do that, everything I've tried so far does not get me anywhere, 'cause I end up changing the $scope.tree itself.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>What I have so far:</p>
  
  <pre><code>app.filter("filterTree", ["$filter", function($filter) {
      return function(tree) {
        var newTree = [];
        angular.forEach(tree, function(node, index) {
          var newNode;
          newNode = {
            id: node.id,
            sort_order: index,
            children: $filter('filterTree')(node.children)
          };
          newTree.push(newNode);
        });
        return newTree;
      };
    }
  ]);
  </code></pre>