4.根据id列表,获取所有子节点的id列表
getChildrenId=(ids)=>{let result =[];const{ trees }=this.state;
ids.forEach(ele=>{let current =this.deepQuery(trees, ele);let allChildren =this.getAllChildren(current?.children);if(allChildren.length !==0){
result.push(...allChildren);}});return _.uniqBy(result,"id");};
5. id数组转换成树结构json
arrayToTree=(list, root)=>{return list
.filter(item=> item.parentId === root).map(item=>({...item,children:this.arrayToTree(list, item.id)}))}let treeData =[]let children =[];
treeData =[{name:'全部',key:'isAll',id:'0',parentId:'-1',children:[...this.arrayToTree(data,'0')]}];[{"id":"1111110021","parentId":"1111110004","layer":2,"name":"二级分类","children":[]},{"id":"1111110031","parentId":"1111110021","layer":3,"name":"三级分类","children":[]},{"id":"1111110004","parentId":"0","layer":1,"name":"jml","children":[]},{"id":"1111110005","parentId":"0","layer":1,"name":"test5566","children":[]},]