この解答例はChatGPTによって生成されています,正しいかは自己判断で。 皆の投稿からも解答例が見つかるかもしれませんよ。
BFE.devでのAI解答例 - JavaScript コーディング問題
104. DOMを階層ごとでリストアップ
以下が解答例です。
const listDOM = (node) => { let result = [node.nodeName]; if (node.children.length > 0) { let childrenResult = []; for (let i = 0; i < node.children.length; i++) { childrenResult.push(listDOM(node.children[i])); } result.push(childrenResult); } return result;}const dom = document.querySelector('html');const domArray = listDOM(dom);console.log(domArray);