Find a node by id in a DOM-like tree
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
attrsandchildrenmay 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");
// => nullNo 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 }, withdepth0 andpath[]for the root. - Make
paththe child indices followed from the root, counting string children as positions. - Skip string children rather than throwing on them, and tolerate a missing
attrsorchildren. - Return
nullwhen nothing matches.
Notes
path.concat(index)instead ofpath.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.
Stuck? The javascript course covers everything this challenge needs.
- 1.finds a nested node and reports its path
- 2.matches the root itself
- 3.returns the first match in pre-order
- 4.tolerates nodes with no attrs or children keys
- 5.returns null when nothing matches
These assertions run in your browser the moment you hit Run. Pass all of them to claim the XP.
Discussion
Sign in to postNo comments yet. Be the first to say something.