title: Converting a flat array into a tree javascript
body: <pre><code>So I have this flat array which has three arrays:
  [
    [
      "a198f9db5e814b11b6e8b885b7978250",
      "New Section",
      0,
      0,
      0,
      0
    ],
    [
      "F0CA2865AA708377EE73A64F98B04BE0",
      "New Section",
      0,
      0,
      2,
      1
    ],
    [
      "B3BADFC6F5A5ECAC1619A651A33C2692",
      "New Section",
      0,
      0,
      14,
      2
    ]
  ]
  </code></pre>
  
  <p>The 5th element in the arrays is my section depth so what I am trying to do is build a tree structure in javascript.But I don't know and understand how to do this. Here is where I am at right now as far as code goes:</p>
  
  <pre><code>    var input = flatArray;
                  var root = {nodes:[], depth: -1};
                  var ancester_branch = [root];
                  var current_target = root;
  
  
                  for(var i = 0; i &lt; input.length; i++){
                      var node = input[i];
                      while(node.depth &lt;= current_target.depth){
                          ancester_branch.pop();
                          current_target = ancester_branch[-1];
                      }
  
                      current_target.nodes.push(node);
                      current_target = node;
                      ancester_branch.push(node);
                      return root;
                  };
  my  var input = rToc.metadata[0].sections;
              var root = {nodes:[], depth: -1};
              var ancester_branch = [root];
              var current_target = root;
  
  
              for(var i = 0; i &lt; input.length; i++){
                  var node = input[i];
                  while(node.depth &lt;= current_target.depth){
                      ancester_branch.pop();
                      current_target = ancester_branch[-1];
                  }
  
                  current_target.nodes.push(node);
                  current_target = node;
                  ancester_branch.push(node);
                  return root;
              };
   var input = rToc.metadata[0].sections;
              var root = {nodes:[], depth: -1};
              var ancester_branch = [root];
              var current_target = root;
  
  
              for(var i = 0; i &lt; input.length; i++){
                  var node = input[i];
                  while(node.depth &lt;= current_target.depth){
                      ancester_branch.pop();
                      current_target = ancester_branch[-1];
                  }
  
                  current_target.nodes.push(node);
                  current_target = node;
                  ancester_branch.push(node);
                  return root;
              };
   var input = rToc.metadata[0].sections;
              var root = {nodes:[], depth: -1};
              var ancester_branch = [root];
              var current_target = root;
  
  
              for(var i = 0; i &lt; input.length; i++){
                  var node = input[i];
                  while(node.depth &lt;= current_target.depth){
                      ancester_branch.pop();
                      current_target = ancester_branch[-1];
                  }
  
                  current_target.nodes.push(node);
                  current_target = node;
                  ancester_branch.push(node);
                  return root;
              };
   var input = rToc.metadata[0].sections;
              var root = {nodes:[], depth: -1};
              var ancester_branch = [root];
              var current_target = root;
  
  
              for(var i = 0; i &lt; input.length; i++){
                  var node = input[i];
                  while(node.depth &lt;= current_target.depth){
                      ancester_branch.pop();
                      current_target = ancester_branch[-1];
                  }
  
                  current_target.nodes.push(node);
                  current_target = node;
                  ancester_branch.push(node);
                  return root;
              };
  </code></pre>
  
  <p>My ancester_branch is to keep track of the nodes as I build the tree and push and pop from it. I don't where to go from here or what to do. So I hope some people can help me out thanks in advance for the help.</p>