🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>[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])); ```