// 将数组转成树 export function array2Tree(array, Id, parentId, children) { const data = [...array]; const result = []; const hash = {}; data.forEach(item => { hash[item[id]] = item; }); data.forEach(item => { // 判断部分数据 自己是根节点的时候自己的pId === Id const hashVP = item[parentId] === item[Id] ? undefined : hash[item[parentId]]; if (hashVP) { if (!hashVP[children]) { hashVP[children] = []; } hashVP[children].push(item); } else { result.push(item); } }); return result; }