企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
>[info] DFS & BFS ``` const doc = { id: 'root', children: [ { id: 'container', children: [ { id: 'sidebar', children: [ { id: 'menu', children: [], } ], }, { id: 'main', children: [ { id: 'post', children: [], }, { id: 'copyright', children: [], } ] } ] } ] } function getKeysDFS(obj = {}, ids = []) { const { id, children = [] } = obj; ids.push(id); let len = children.length; if (len) { for(let i = 0; i < len; i++) { getKeysDFS(children[i], ids); } } return ids; } console.log("idsDFS", getKeysDFS(doc)); function getKeysBFS(arrs = [], ids = []) { let arrsChild = []; let len = len = arrs.length; if (!len) { return; } for(let i = 0; i < len; i++){ const { id, children = [] } = arrs[i]; ids.push(id); arrsChild = [...arrsChild, ...children]; } getKeysBFS(arrsChild, ids); return ids; } console.log("idsBFS", getKeysBFS([doc])); ```