title: Recursive javascript for nested comments
body: <p>I'm trying to recursively loop through nested comment data such as...</p>
  
  <pre><code>-parent comment a
  ----child comment a
  --------grandchild comment a
  ----child comment b
  -parent comment b
  </code></pre>
  
  <p>And here's the outline of what I'm doing in javascript...</p>
  
  <pre><code>loadChildComments(commentdata[i].comment_id); //for every parent comment I make this call to load children
  
  function loadChildComments(parentId){
  
    for (k=0; k&lt;commentdata.length; k++) { //loop through the same data set looking for child comments
      if (commentdata[k].immediate_parent_id == parentId) {
        //we've found a child comment
  
        //bunch of steps to add the child comment to the page
  
        ///just finished adding a child comment so loop through to see if that child itself has children
        loadChildComments(commentdata[k].comment_id); //also tried arguments.callee(commentdata[k].comment_id);
  
      }
    }
    return true;
  }
  </code></pre>
  
  <p>If I don't add the recursive callback to loadChildComments() at the end, I correctly get...</p>
  
  <pre><code>-parent comment a
  ----child comment a
  ----child comment b
  -parent comment b
  </code></pre>
  
  <p>But when I do include the callback I get this...</p>
  
  <pre><code>-parent comment a
  ----child comment a
  --------grandchild comment a
  -parent comment b
  </code></pre>
  
  <p>(missing "child comment b")</p>
  
  <p>So it seems the recursive call is working, but javascript doesn't pick up where it previously was when it finishes the recursive call?</p>
  
  <p>Pretty sure I'm missing something fundamental about the way recursive loops work in javascript but no idea what after doing a little research and trying various solutions to no avail.  Thanks for any ideas whatsoever!</p>