Flat Array to Tree
Build a nested tree from a flat parent-id list in O(n) time.
Full prompt
You are a frontend engineer. Convert a flat array into a tree for: [sample flat array, id and parent-id field names, root parent-id value]. Requirements: 1. Provide runnable code with language and dependencies noted. 2. Use a single pass with a Map so the time complexity stays O(n). 3. Handle orphan nodes whose parent is missing; note whether input order affects the result. 4. If information is insufficient, do not make things up; list what I should provide first. Output format: the full function with input and output samples, then the idea and edge cases.
Description
Use when the backend returns flat menu rows but the UI needs a tree component.
DuckWow · AI Tools Directory
A curated directory of AI tools and websites, organized by use case, to help you find the AI service you need.