export default function listToTreeData(listData: any) { function setChildrenToId(list: any, targetId: any, chilren: any) { list.forEach((ele: any) => { if (ele.uid == targetId) { if (!ele.children) { ele.children = []; } let isIn = false; ele.children.forEach((menu: any) => { if (menu.uid == chilren.uid) { isIn = true; } }); !isIn && ele.children.push(chilren); } else { if (ele.children) { setChildrenToId(ele.children, targetId, chilren); } } }); } listData.forEach((element: any) => { const parentUid = element.parentUid; if (parentUid && (parentUid !== 0 || parentUid !== "0")) { setChildrenToId(listData, parentUid, element); } }); listData = listData.filter((ele: any) => { return ele.parentUid == "0" || !ele.parentUid; }); //这一步是过滤,按树展开,将多余的数组剔除; return listData; }