其他分享
首页 > 其他分享> > 把从后台获取的平级数据转换成树状结构数据;

把从后台获取的平级数据转换成树状结构数据;

作者:互联网

比如当后台返回如下的平级数据:

 data(){
    return {
      processedData:[
        {COMCODE:'CLUB00000000',COMNAME:'会员俱乐部',GRADE:'01',PARENTCOMCODE:'0000'},
        {COMCODE:'AH066340000',COMNAME:'安徽公司',GRADE:'02',PARENTCOMCODE:'CLUB00000000'},
        {COMCODE:'AH066340001',COMNAME:'宿州恒大名都',GRADE:'03',PARENTCOMCODE:'AH066340000'},
        {COMCODE:'AH066340002',COMNAME:'合肥恒大水晶国际广场',GRADE:'03',PARENTCOMCODE:'AH066340000'},
        {COMCODE:'AH066340003',COMNAME:'六安恒大御景湾',GRADE:'03',PARENTCOMCODE:'AH066340000'},
        {COMCODE:'BJ066110000',COMNAME:'北京公司',GRADE:'02',PARENTCOMCODE:'CLUB00000000'},
        {COMCODE:'BJ066110001',COMNAME:'邯郸恒大绿洲',GRADE:'03',PARENTCOMCODE:'BJ066110000'},
        {COMCODE:'BJ066110002',COMNAME:'邯郸恒大翡翠华庭',GRADE:'03',PARENTCOMCODE:'BJ066110000'},
        {COMCODE:'BJ066110003',COMNAME:'唐山恒大学庭',GRADE:'03',PARENTCOMCODE:'BJ066110000'}
      ]
    }
  }

而我们需要得到的数据效果如下树状结构数据:

processedData:[
        {
          COMCODE:'CLUB00000000',
          COMNAME:'会员俱乐部',
          GRADE:'01',
          PARENTCOMCODE:'0000',
          children:[
            {
              COMCODE:'AH066340000',
              COMNAME:'安徽公司',
              GRADE:'02',
              PARENTCOMCODE:'CLUB00000000',
              children:[
                {COMCODE:'AH066340001',COMNAME:'宿州恒大名都',GRADE:'03',PARENTCOMCODE:'AH066340000'},
                {COMCODE:'AH066340002',COMNAME:'合肥恒大水晶国际广场',GRADE:'03',PARENTCOMCODE:'AH066340000'},
                {COMCODE:'AH066340003',COMNAME:'六安恒大御景湾',GRADE:'03',PARENTCOMCODE:'AH066340000'}
              ]
            },
            {
              COMCODE:'BJ066110000',
              COMNAME:'北京公司',
              GRADE:'02',
              PARENTCOMCODE:'CLUB00000000',
              children:[
                {COMCODE:'BJ066110001',COMNAME:'邯郸恒大绿洲',GRADE:'03',PARENTCOMCODE:'BJ066110000'},
                {COMCODE:'BJ066110002',COMNAME:'邯郸恒大翡翠华庭',GRADE:'03',PARENTCOMCODE:'BJ066110000'},
                {COMCODE:'BJ066110003',COMNAME:'唐山恒大学庭',GRADE:'03',PARENTCOMCODE:'BJ066110000'}
              ]
            }
          ]
        }
      ]

那么如何优雅的实现这种效果呢,代码如下:

 

标签:BJ066110000,03,树状,GRADE,平级,COMNAME,COMCODE,PARENTCOMCODE,数据
来源: https://www.cnblogs.com/rxqlx/p/10394154.html