AniUI Academy

Find a node by id in a DOM-like tree

mediumDOM & Utilities+250 XPPractice

Renderers, MDX pipelines and test utilities all keep a plain-object description of a document before it becomes real elements. Searching that structure is the same depth-first walk the browser does, and writing it makes the tree/traversal relationship concrete.

A node is a plain object { tag, attrs, children }. attrs is a plain object of attribute values and may be missing entirely. children is an array that may be missing, and it may hold either child nodes or plain strings for text content.

Implement findById(root, id). Search depth-first in pre-order — check a node before its children, and children left to right — and return a summary of the **first** node whose attrs.id equals id: { tag, depth, path }. depth is 0 for the root and one more for each level down. path is the array of child indices you followed from the root, so the root itself gives [].

String children are text, never match, and are skipped — but they still occupy their index, so the indices in path are positions in the raw children array. Return null when no node matches.

Arguments

root{ tag: string; attrs?: object; children?: Array }
The root node of a plain-object element tree. Both attrs and children may be omitted; a child may itself be a plain string of text.
idstring
The exact id to find in attrs.id.

Returns

{ tag: string; depth: number; path: number[] } | null — A summary of the first matching node — its tag, depth from the root, and the child-index path — or null when nothing matches.

Examples

const tree = { tag: "div", attrs: { id: "root" }, children: [
  "text",
  { tag: "p", attrs: {}, children: ["hello"] },
  { tag: "section", attrs: { id: "main" }, children: [
    { tag: "span", attrs: { id: "target" }, children: [] }
  ] }
] };
findById(tree, "target");
// => { tag: "span", depth: 2, path: [2, 0] }

The path indexes children directly, so string children at index 0 still count.

findById({ tag: "div", attrs: { id: "root" }, children: [] }, "root");
// => { tag: "div", depth: 0, path: [] }

A root match has depth 0 and the empty path.

findById({ tag: "div", attrs: {}, children: ["only text"] }, "missing");
// => null

No match returns null rather than throwing.

What it has to do

  • Search depth-first in pre-order and return the first match.
  • Return { tag, depth, path }, with depth 0 and path [] for the root.
  • Make path the child indices followed from the root, counting string children as positions.
  • Skip string children rather than throwing on them, and tolerate a missing attrs or children.
  • Return null when nothing matches.

Notes

  • path.concat(index) instead of path.push(index) is what keeps each recursive branch's path independent — mutating the shared array would leak state back up the stack.
  • This mirrors the browser's getElementById, but the browser also indexes the document for O(1) lookup. A plain-tree walk here is O(n); for repeated lookups, build a map keyed by id first.
Asked atMicrosoftAtlassian

Stuck? The javascript course covers everything this challenge needs.

Try it yourself
Loading playground...
5 tests · +250 XP

Discussion

Sign in to post
Sign in to join the conversation.Sign in

No comments yet. Be the first to say something.