Skip to content

Instantly share code, notes, and snippets.

@scolton99
Created June 28, 2021 19:18
Show Gist options
  • Select an option

  • Save scolton99/1f820c31858aaf367a7165c182f4e926 to your computer and use it in GitHub Desktop.

Select an option

Save scolton99/1f820c31858aaf367a7165c182f4e926 to your computer and use it in GitHub Desktop.
A sketch of way to generate a decision tree on a website.
const selector = "#root > li";
const tree = {
dom_el: null,
text: 'root',
children: []
};
const gen_tree = () => {
const init = Array.from(document.querySelectorAll(selector));
const dfs = (parent_node, dom_node) => {
const tree_node = {
dom_el: it,
text: it.textContent,
children: []
};
parent_node.children.push(tree_node);
const { children } = dom_node;
if (children.length === 0)
return;
for (const child of children) {
if (child.tagName !== "UL")
continue;
const child_lis = child.children;
for (const child_li of child_lis)
dfs(tree_node, child_li);
break;
}
};
init.forEach(it => dfs(tree, init));
console.log(tree);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment