Trees and recursionintermediate
A flat list with a parentId is a tree pretending not to be one, and turning it into real nesting is the same problem behind threaded comments, category menus, org charts and file browsers.
The API returns a flat list with parentId. The UI needs it nested. This is the same problem as a category menu, an org chart and a file browser.
The nesting is right, the path is right, and 20,000 comments nest inside the budget.
Published in full, before you start — every point is one of these and there is nothing else. Each one runs your code; it is not a search for keywords.
A flat list with a parentId is a tree pretending not to be one, and turning it into real nesting is the same problem behind threaded comments, category menus, org charts and file browsers.
A search inside a loop does the work once per row per row: at 20,000 rows that is 400 million comparisons, and it is the single most common reason a working feature is unusably slow.