正常数组转换为树状结构

1、这里子元素的标识是menuId,父id是parentId

//转化后的树形结构数据
            getTree(menuList) {
                let menus = [];
                let sourceMap = {};
                menuList.forEach(item => {
                  item.children = [];
                  sourceMap[item.menuId] = item; 
                  if (Number(item.parentId) === 0) {
                    menus.push(item);
                  } else {
                    if (sourceMap[item.parentId]) {
                        if(item.path.indexOf('developing') > -1){
                            item.development = false;
                        }else{
                            item.development = true;
                        }
                      sourceMap[item.parentId].children.push(item);
                    }
                  }
                })
                return menus;
            },

 

posted on 2024-01-26 14:14  小虾米吖~  阅读(10)  评论(0编辑  收藏  举报