title: Printing a tree structure in a list like manner, storing the indent strings (which consist of┣, ┃, ┗ )
body: <p>I want to store the "node indentation string" for each object, something like this:</p>
  
  <pre><code>foo
  ┣bar
  ┃┗baz
  ┃ ┗qux
  ┃  ┣quux
  ┃  ┗corge
  ┣fizz
  ┗buzz
  </code></pre>
  
  <p>Given data for each object:</p>
  
  <pre><code>objects = [
  {'id':1,'parent_id':null, 'name':'foo'}
  {'id':2,'parent_id':1, 'name':'bar'}
  ];
  </code></pre>
  
  <p>Note that I don't want to print anything, I just want to work out the <code>indent</code> as an array of characters for each object:</p>
  
  <p><code>{'id':6,'parent_id':4, 'name':'corge', 'indent':['┃',' ',' ','┗']}</code></p>
  
  <p>So far I can only indent them with spaces but no 'pipes' and I am stumped at coming up with a solution. Any help?</p>
  
  <p>I am using JS with Angular if it helps.</p>
  
  <p>EDIT: As requested the code I have so far. I didn't post this at first because I felt that it's a wrong foundation/approach to build on. How it works is pretty trivial: for each object, count it's ancestors and add <code>" "</code>'s accordingly.</p>
  
  <pre><code>// go through all our objects and set their indent strings
  setIndents = function()
  {
      for (var x in objects) {
          var o = objects[x];
          o.nodes = [];
  
          // push space character for amount of ancestors
          numParents = countParents(o, 0);
          for (var i = 0; i &lt; numParents; i++)
              o.nodes.push(" ");
      }
  };
  // recursively counts how many ancestors until we hit the root
  countParents = function(current, count)
  {
      if (current.parent_id !== null) {
          for (var x in objects) {
              if (objects[x].id == current.parent_id) {
                  current = objects[x]; //set as new current
                  count++;
                  break;
              }
          }
          return countParents(current, count);
      } else {
          return count;
      }
  
  };
  </code></pre>