多层数据 转换 属性名

主要方法

function render(data) {
                  return data ? data.map(item => {
                      if(item.hasChildren){
                          return {
                            value: item.id,
                            text: item.title,
                            children: this.render(item.children)
                          }
                      }else{
                          return {
                            value: item.id,
                            text: item.title,
                            // children: this.render(item.children)
                          }
                      }
                      
                  }): null
              }

使用

let data=[
{
    "title": "北京市",
    "id": "110000",
    "children": [{
        "title": "市辖区",
        "id": "110100",
        "children": [{
            "title": "东城区",
            "id": "110101"
        }]
    }]
},
{
    "title": "山西省",
    "id": "140000",
    "children": [{
        "title": "太原市",
        "id": "140100",
        "children": [{
            "title": "市辖区",
            "id": "140101"
        }]
    }]
},
]
let  renderData=render(data)
console.log(renderData)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容