title: How to recursively build the path of sub-arrays inside an array with unknown depth
body: <p>I have an array structured like the following:</p>
  
  <pre><code>var persons= [{name: 'John',
                 id: 1,
                 children :[{name :'John Jr',
                             id: 11,
                             children :[{name: 'John Jr Jr'
                                        id: 111}]
                            },
                            {name :'Jane'
                             id: 12}]
               },
               {name:'Peter',
                id :2
               },
               ...]
  </code></pre>
  
  <p>It is basically an array of objects, the objects are persons, each person may have any number of descendants which are by them selves arrays of person objects. The number of descendants of each person is unknown.</p>
  
  <p>What I am trying to achieve is to have a map structured this way:</p>
  
  <pre><code>var personsMap =[1 : 'John',
              11: 'John &gt; John Jr',
              111 : 'John &gt; John Jr &gt; John Jr Jr',
              12: 'John &gt; Jane',
              ...
             ]
  </code></pre>
  
  <p>It is a map of each possible combination of each path, so by querying the map by the person's id, it should return the string of it's parent > grand-parent >...</p>
  
  <p>I'am trying to build this map recursively so what I have tried so far:</p>
  
  <pre><code>var personsMap = {};
  
  function buildParentsMap (persons){
     $.each (persons, function(ndx, person){
        if(person.children &amp;&amp; person.children.length &gt; 0){
            buildParentsMap(person.children);
        }
        personsMap[person.id] = person.name; 
     });
     console.log(personsMap);
  }
  </code></pre>
  
  <p>But this outputs the following :</p>
  
  <pre><code>[1 : 'John',
  11: 'John Jr',
  111 'John Jr Jr',
  12: 'Jane',
  ...]
  </code></pre>
  
  <p>It is all the names but without being concatenated the way I explained above.
  How can I achieve this? Thanks</p>