Open Mrxiaoweitongxue opened 1 year ago
let arr = [ { id: 1, name: '部门1', pid: 0 }, { id: 2, name: '部门2', pid: 1 }, { id: 3, name: '部门3', pid: 1 }, { id: 4, name: '部门4', pid: 3 }, { id: 5, name: '部门5', pid: 4 }, { id: 6, name: '部门6', pid: 0 }, ] //用了个递归,菜鸟瑟瑟发抖 list2tree=(arr,id)=>{ let res = [] for(let i =0;i<arr.length;i++){ if(arr[i].pid==id){ res.push(arr[i]) arr[i].children=list2tree(arr,arr[i].id) arr.splice(i,1) --i } } return res }
let arr = [ { id: 1, name: '部门1', pid: 0 }, { id: 2, name: '部门2', pid: 1 }, { id: 3, name: '部门3', pid: 1 }, { id: 4, name: '部门4', pid: 3 }, { id: 5, name: '部门5', pid: 4 }, { id: 6, name: '部门6', pid: 0 }, ] //用了个递归,菜鸟瑟瑟发抖 list2tree=(arr,id)=>{ let res = [] for(let i =0;i<arr.length;i++){ if(arr[i].pid==id){ res.push(arr[i]) arr[i].children=list2tree(arr,arr[i].id) arr.splice(i,1) --i } } return res }