#Recursion#Trees

Nested Tabs From a Flat Array

Given a flat array of items with parentId, render them as nested tabs at arbitrary depth.

By Pratik RaiMedium

You are handed a flat array of { id, name, parentId } and asked to render it as a nested tab or accordion structure, with each item appearing under its parent.

The whole problem is recognising it is two problems. Trying to render nesting directly from the flat array leads to filtering the array once per level, which is quadratic and impossible to write for unknown depth. Transform first, then render.

The transform is the standard one: build a lookup of id → node, each with an empty children array, then walk the list again and push every node into its parent. Doing it in two passes is what makes the input order irrelevant — a child listed before its parent still finds it — and that is worth saying out loud, because the single-pass version needs extra handling for forward references.

Rendering is then mutual recursion: a level renders items, and an item renders a level for its children. Depth stops being something the code knows about.

The data here is deliberately three levels deep, because a two-level implementation passes a two-level test and fails the moment anything nests further.

A good follow-up to expect: a real tab list moves selection with the arrow keys, and genuinely nested navigation is usually better served by role="tree" than by tabs. Knowing which pattern you are actually building is part of the answer.

Goal: Turn the flat list into a tree, then render it recursively with selection and collapsing.

Source

Frequently asked questions

Can you render nesting directly from a flat array?
Not workably. You would have to filter the array once per level, which is quadratic and cannot be written for unknown depth. Convert to a tree first, then render recursively.
Why build the id lookup before linking nodes?
So input order stops mattering. With every node in the lookup before any linking begins, a child listed before its parent still finds it — a single pass needs extra handling for that forward reference.
How deep should the implementation support?
Arbitrary depth, which recursion gives you for free. A two-level implementation passes a two-level test and breaks on the first three-level dataset, so test with one deliberately deeper than the example.
Should this be tabs or a tree?
Genuinely nested navigation is usually better described by `role="tree"` than by a tab list, and a real tab list also moves selection with the arrow keys. Recognising which pattern you are building is a common follow-up.

Related Challenges

Continue learning with these related challenges

View All
React

File Explorer

Build a nested folder tree from nothing — expand, collapse, and add files and folders at any depth.

React · JavaScriptPratik Rai ·

React

Pagination With Ellipsis

A pagination control that always shows the first, last and current pages, collapsing every gap into a single ellipsis.

React · JavaScriptPratik Rai ·

React

Accessible Dropdown

A select built from scratch — keyboard navigation, focus management, outside-click and the full ARIA listbox contract.

React · JavaScriptPratik Rai ·