Skip to main content

Extract Nodes by Type

MIDDLE
EPAMLuxoftGrammarly

Write a function extractNodesByType(root, type) that takes a DOM tree root and node type, and returns all nodes of that type in a flat array.

Each node has a tagName property and optionally a children array containing child nodes.

Example:

Input: root = { tagName: "div", children: [{ tagName: "span" }, { tagName: "p" }] }, type = "span"
Output: [{ tagName: "span" }]

Examples:

Input 1:{"root":{"tagName":"div","children":[{"tagName":"span"},{"tagName":"p"}]},"type":"span"}
Output 1:[{"tagName":"span"}]
Input 2:{"root":{"tagName":"div","children":[]},"type":"span"}
Output 2:[]

Loading editor...

Run your code to see results

Click the Run button above