[/ / / / let the list = {' id ':' 3129 ', 'pid' : ', 'name' : 'President'}, / / {' id ':' 312 ', 'pid' : ', 'name' : 'general manager'}, / / {" id ": '312 d', 'pid', '312' c, 'name' : 'financial accounting department}, / / {' id' : '312 e', 'pid' : '312 c', 'name' : 'tax department}, / / {' id' : '312 f', 'pid' : '312 c', 'name' : 'salary management'}, / / {' id ':' 00 d2 ', 'pid' : ', 'name' : 'technology'}, / / {' id ':' 00 d3 ', 'pid' : '00 d2', 'name' : 'product research and development department}, / / {' id' : '00 d4', 'pid' : '00 d2', 'name' : 'advertising r&d'}, / / {' id ':' 00 d5 ', 'pid' : '00 d2', 'name' : 'shape r&d'}, / / {' id ':' 00 d6 ', 'pid' : ', 'name' : 'operations'}, / / {' id' : '00 d7', 'pid' : ', 'name' : 'marketing'}, / / {" id ": '00 d8', 'pid' : '00 d7', 'name' : 'shenzhen division'}, / / {' id ':' 00 d9 ', 'pid' : '00 d7', 'name' : 'Shanghai business'} / /]Copy the code
Function terrList(list) {const treeList = [] const map = {} List. ForEach (item => {if (! Item.children) {item.children = []} map[item.id] = item list. ForEach (item => {item.children) {item.children = []} map[item.id] = item list. // If no parent is found, it has no parent. TreeList const parent = map[item.pid] // If (parent) {parent.children.push(item)} else {// TreeList. Push (item)}}) // return treeList}Copy the code