ts 遍历树形数组 找出某一项

function deepQuery(tree:any,id:any){
   let isGet = false;
   let retNode = {title:''}; 
   function deepSearch(tree:any,id:any){
     for(var i = 0;i<tree.length;i++){
        if(tree[i].children && tree[i].children.length > 0){
           deepSearch(tree[i].children,id);
        }
        if(id === tree[i].id || isGet){
            isGet || (retNode = tree[i]);
            isGet = true;
            break;
        }
      }
   }
     deepSearch(tree,id);
     return retNode;
}

 转载自:https://www.benbonben.com/

posted @ 2022-01-14 16:10  BenBonBen  阅读(752)  评论(0编辑  收藏  举报