JavaScript遍历树结构

遍历

function parseTreeJson(treeNodes){
    if(!treeNodes||!treeNodes.length)return;
    for(let i=;i<treeNodes.length;i++){
        let childs=treeNodes[i].children;
        console.log(treeNodes[i].key)
        if(childs&&childs.length>){
            parseTreeJson(childs);
        }
    }
}

广度非递归遍历

const travelWidely=(roots)=>{
    const queue = [...roots];
    while (queue.length){
        const node = queue.shift();
        //打印被遍历的节点
        if(node===undefined) return;
        console.log(`${node.title} ${node.children && node.children.length || ""}`);

        if(node.children&&node.children.length){
            queue.push(...node.children)
        }
    }
}