export interface TreeNode { key: string | number; title: string; children?: TreeNode[]; raw?: T; } export function buildTree(list: T[]): TreeNode[] { const map = new Map>(); const roots: TreeNode[] = []; list.forEach(item => { map.set(item.id, { key: item.id, title: (item as any).title || item.name || String(item.id), raw: item, children: [] }); }); list.forEach(item => { const node = map.get(item.id)!; if (item.pId && map.has(item.pId)) { map.get(item.pId)!.children!.push(node); } else { roots.push(node); } }); return roots; }