How do I create a function that properly formats the DOM for a 'nested' comment section
<p>I'm working on a module that includes <em>nested comments</em>. The data returned from my <code>ajax</code> request is formatted, roughly, like this:</p>

<pre><code>  items: Array[3]
      0: Object
         Children: Array[3]
         0: Object
            Children: Array[2]
       1: Object
         Children: Array[3]
         0: Object
            Children: Array[2]
</code></pre>

<p>I've written a recursive function that finds all the parent and child comments, and appends the list to another element on the page:</p>

<pre><code>    function findChildren( root ) {

      if( root.length &gt;= 1 ) {

         $.each( root, function( key, parent ) {

              units.comments.push( '&lt;div id="root-comment-' + parent.id + '"&gt;' + parent.comment  + '&lt;/div&gt;' );

              if( parent.children.length &gt;=1 ) {

                $.each( parent.children, function( key, child ) {

                    units.comments.push( '&lt;div id="child-comment-' + child.id + '"&gt;' + child.comment  + '&lt;/div&gt;' );

                    if( child.children.length &gt;= 1 &amp;&amp; child.children != null ) {

                        findChildren( child.children );

                      }

                  });

               } 

            });
     }

   } findChildren( parents );
</code></pre>

<p>The <strong>problem</strong> with  <code>findChildren</code> is that is appends a flat list of elements. <strong>EDIT</strong> I know I'm closing the <code>&lt;div id="root-comment"&gt;</code>.</p>

<pre><code>   &lt;div id="parent-comment"&gt;&lt;p&gt;Some Comment&lt;/p&gt;&lt;/div&gt;
   &lt;div id="child-comment"&gt;&lt;p&gt;Some Comment&lt;/p&gt;&lt;/div&gt;
</code></pre>

<p>What I would like is a nested list of elements</p>

<pre><code>   &lt;div id="parent-comment"&gt;&lt;p&gt;Some Comment&lt;/p&gt;
       &lt;div id="child-comment"&gt;&lt;p&gt;Some Comment&lt;/p&gt;&lt;/div&gt;
   &lt;/div&gt;
</code></pre>

<p>Any help would be appreciated.</p>
