title: Recursive function to create a list from another deep list
body: <p>Another question about recursive function, I cant get my head arround them.
  I have a list with groups that can have any depth, an example:</p>
  <pre><code>{
      Id: 1,
      Name:&quot;Root&quot;,
      Children: [
        {
          Id: 1,
          Name:&quot;&quot;,
          Children: [    
            {
             Id: 1,
             Name:&quot;&quot;,
             Children: [
                 {
                  Id: 1,
                  Name:&quot;&quot;,
                  Children: []
                 },
               ]
           },
          ]
        },
        {
          Id: 2,
          Name:&quot;&quot;,
          Children: []
        },
        {
          Id: 3,
          Name:&quot;&quot;,
          Children: []
        },
      ]
  }
  </code></pre>
  <p>I show these groups in a dropdown that the user can select.
  What I need to do is when the user clicks on any group, I need to show all users that are a part of that group <strong>AND</strong> its subgroups.</p>
  <p>The information about which users belong to the group and its subgroups is hold by the userlist. That list is flat and every user has an prop that contains an membership array.</p>
  <p>I have re-written this method below several times, this is the closest I get, but this more than doubles the expected lenght because I get dublicates.</p>
  <pre><code>const getAllUsersInGroup = (group, usersFiltered) =&gt; {
      if (!group.Children.length) return usersFiltered.flat();
  
      return group.Children.flatMap((g) =&gt; {
          return getAllUsersInGroup(
              g,
              [...usersFiltered, users.filter((u) =&gt; u.Memberships.some((m) =&gt; m.GroupId === g.Id))]
          );
      });
  };
  </code></pre>
  <p>Another test returns almost all but there is missing users on bigger groups with many subgroups.</p>
  <pre><code>const getAllUsersInGroup = (group, userss) =&gt; {
      if (!group.Children.length) return [...userss].flat();
  
      return group.Children.flatMap((g) =&gt; {
          return getAllUsersInGroup(g, 
              users.filter((u) =&gt; u.Memberships.some((m) =&gt; m.GroupId === g.Id)),
          );
      });
  };
  </code></pre>
  <p>I must be stuck in some wrong thinking or just pure stupid..
  Maybe I dont need to check the Children lenght and just go thro them all, but as I understand it you need some statment that stops the method.</p>
  <p>A little help would be much appreciated!</p>
  <p>Regards</p>