香菇怪 2022-04-28 15:42 采纳率: 100%
浏览 113
已结题

想问一下从后端得到如下所示数据,怎么转换为el-menu的三级菜单的数据结构(面试时遇到的笔试题没写出来)(语言-javascript)

后端传入的数据

list = [
  {"ID": 1,"MenuOne": "教学楼","MenuTwo": "A01教室","NoteName": "A01前"},
  {"ID": 2,"MenuOne": "教学楼","MenuTwo": "A01教室","NoteName": "A01后"},
  {"ID": 3,"MenuOne": "教学楼","MenuTwo": "A02教室","NoteName": "A02前"},
  {"ID": 4,"MenuOne": "教学楼","MenuTwo": "A02教室","NoteName": "A02后"},
  {"ID": 5,"MenuOne": "操场","MenuTwo": "跑道","NoteName": "跑道前"},
  {"ID": 6,"MenuOne": "操场","MenuTwo": "跑道","NoteName": "跑道后"},
  {"ID": 7,"MenuOne": "操场","MenuTwo": "厕所","NoteName": "厕所前"},
  {"ID": 8,"MenuOne": "操场","MenuTwo": "厕所","NoteName": "厕所后"},
  {"ID": 9,"MenuOne": "食堂","MenuTwo": "","NoteName": "食堂前"},
  {"ID": 10,"MenuOne": "食堂","MenuTwo": "","NoteName": "食堂后"},
  {"ID": 11,"MenuOne": "食堂","MenuTwo": "","NoteName": "食堂左"},
  {"ID": 12,"MenuOne": "食堂","MenuTwo": "","NoteName": "食堂右"},
]
我想要达到的结果
menu = [
  {
    id:'1',
    label:'教学楼',
    child:[
      {
        label:'A01教室',
        child:[
          {id:'1-1',label:'A01前'},
          {id:'1-2',label:'A01后'}
        ]
      },
      {
        label:'A02教室',
        child:[
          {id:'1-3',label:'A02前'},
          {id:'1-4',label:'A02后'}
        ]
      },
    ]
  },
  {
    id:'2',
    label:'操场',
    child:[
      {
        label:'跑道',
        child:[
          {id:'2-1',label:'跑道前'},
          {id:'2-2',label:'跑道后'}
        ]
      },
      {
        label:'厕所',
        child:[
          {id:'2-3',label:'厕所前'},
          {id:'2-4',label:'厕所后'}
        ]
      },
    ]
  },
  {
    id:'3',
    label:'食堂',
    child:[
      {
        label:'',
        child:[
          {id:'3-1',label:'食堂前'},
          {id:'3-2',label:'食堂后'},
          {id:'3-3',label:'食堂左'},
          {id:'3-4',label:'食堂右'}
        ]
      }
    ]
  }
]

  • 写回答

4条回答 默认 最新

  • Heerey525 前端领域新星创作者 2022-04-28 16:27
    关注
    
    var list = [
            { ID: 1, MenuOne: "教学楼", MenuTwo: "A01教室", NoteName: "A01前" },
            { ID: 2, MenuOne: "教学楼", MenuTwo: "A01教室", NoteName: "A01后" },
            { ID: 3, MenuOne: "教学楼", MenuTwo: "A02教室", NoteName: "A02前" },
            { ID: 4, MenuOne: "教学楼", MenuTwo: "A02教室", NoteName: "A02后" },
            { ID: 5, MenuOne: "操场", MenuTwo: "跑道", NoteName: "跑道前" },
            { ID: 6, MenuOne: "操场", MenuTwo: "跑道", NoteName: "跑道后" },
            { ID: 7, MenuOne: "操场", MenuTwo: "厕所", NoteName: "厕所前" },
            { ID: 8, MenuOne: "操场", MenuTwo: "厕所", NoteName: "厕所后" },
            { ID: 9, MenuOne: "食堂", MenuTwo: "", NoteName: "食堂前" },
            { ID: 10, MenuOne: "食堂", MenuTwo: "", NoteName: "食堂后" },
            { ID: 11, MenuOne: "食堂", MenuTwo: "", NoteName: "食堂左" },
            { ID: 12, MenuOne: "食堂", MenuTwo: "", NoteName: "食堂右" },
          ];
    
          let arr = [];
    
          list.map((item) => {
            let isHasOne = arr.findIndex((ele) => ele.label == item.MenuOne);
            if (isHasOne === -1) {
              let id = (arr.length + 1).toString();
              arr.push({
                id,
                label: item.MenuOne,
                child: [
                  {
                    label: item.MenuTwo,
                    child: [
                      {
                        id: id + "-1",
                        label: item.NoteName,
                      },
                    ],
                  },
                ],
              });
            } else {
              let isHasTwo = arr[isHasOne].child.findIndex(
                (ele) => ele.label == item.MenuTwo
              );
              let sub = arr[isHasOne].child.reduce((a, b) => {
                let result = b.child.concat(a.child);
                b.child = result;
                return b;
              });
              if (isHasTwo === -1) {
                arr[isHasOne].child.push({
                  label: item.MenuTwo,
                  child: [
                    {
                      id: arr[isHasOne].id + "-" + (sub.child.length + 1),
                      label: item.NoteName,
                    },
                  ],
                });
              } else {
                arr[isHasOne].child[isHasTwo].child.push({
                  id: arr[isHasOne].id + "-" + (sub.child.length + 1),
                  label: item.NoteName,
                });
              }
            }
          });
    
          console.log("arr", arr);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月6日
  • 已采纳回答 4月28日
  • 赞助了问题酬金20元 4月28日
  • 创建了问题 4月28日

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含