树型结构修改属性名

279 阅读1分钟

一、树型结构修改属性名

例如:要把如下数据结构改成我们常用的树型结构数据

let arr = [
  {
    regionType: 1,
    regionTypeName: '北京大区',
    dealerList:[
      {
        divisionName: '北京-瑞森大厦',
        divisionCode: 11,
        warehouseList: [
          {
            dealerName: '门店禁用仓库1',
            dealerCode: 111
          },
          {
            dealerName: '门店禁用仓库2222',
            dealerCode: 222
          }
        ]
      }
    ],

  }
];
let obj = JSON.parse(JSON.stringify(arr).replace(/regionTypeName/g, 'label').replace(/regionType/g, 'value')
  .replace(/dealerList/g, 'children')
  .replace(/divisionName/g, 'label')
  .replace(/divisionCode/g, 'value')
  .replace(/warehouseList/g, 'children')
  .replace(/dealerName/g, 'label')
  .replace(/dealerCode/g, 'value'));

this.treeData = {
  children: obj
};

2. 修改后 treeData 如下

{
    "children": [
        {
            "value": 1,
            "label": "北京大区",
            "children": [
                {
                    "label": "北京-瑞森大厦",
                    "value": 11,
                    "children": [
                        {
                            "label": "门店禁用仓库1",
                            "value": 111
                        },
                        {
                            "label": "门店禁用仓库2222",
                            "value": 222
                        }
                    ]
                }
            ]
        }
    ]
}
image.png