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

想问一下从后端得到如下所示数据,怎么转换为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条回答 默认 最新

相关推荐 更多相似问题