Created
June 28, 2021 19:18
-
-
Save scolton99/1f820c31858aaf367a7165c182f4e926 to your computer and use it in GitHub Desktop.
A sketch of way to generate a decision tree on a website.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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