利用map对象存储将数组转换为树形结构

// util.transTree = (items,idDepartment) => {
//   const result = []; // 结果集
//   const itemMap = {};
 
//   // 先转成map存储
//   for (const item of items) {
   
//       itemMap[item.id] = { ...item, users: [] };
       
//   }

//   console.log(itemMap)
 
//   for (const item of items) {
//     const id = item.id;
   
//     if(item.parent_id){
//       var parentId = item.parent_id;
//     }
//     else{
//       var parentId = item.group_id;
//     }
//     const treeItem = itemMap[id];
//     // console.log(parentId)
//     if (parentId === idDepartment) {
//       result.push(treeItem);
//     } else {
     
//       // if (!itemMap[parentId]) {
//       //   itemMap[parentId] = { users: [] };
//       // }
//       // itemMap[parentId].users.push(treeItem);
//     }
//   }
//   return result;
// };
posted @ 2022-11-10 13:59  踏浪小鲨鱼  阅读(223)  评论(0)    收藏  举报