首頁 > 軟體

JS前端二維陣列生成樹形結構範例詳解

2022-09-16 22:03:44

問題描述

前端在構建國家的省市區結構時,介面返回的不是樹形結構,這個時候就需要我們進行轉化。以下資料為例

 [
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872922,
            "parentCode": "000001",
            "nodeCode": "000001001",
            "name": "杭州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872940,
            "parentCode": "000001001",
            "nodeCode": "000001001001",
            "name": "上城區",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872922,
            "parentCode": "000001",
            "nodeCode": "000001001",
            "name": "杭州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872961,
            "parentCode": "000001001",
            "nodeCode": "000001001002",
            "name": "下城區",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872922,
            "parentCode": "000001",
            "nodeCode": "000001001",
            "name": "杭州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533872980,
            "parentCode": "000001001",
            "nodeCode": "000001001003",
            "name": "臨安區",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873196,
            "parentCode": "000001",
            "nodeCode": "000001002",
            "name": "寧波市",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873432,
            "parentCode": "000001",
            "nodeCode": "000001003",
            "name": "溫州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873468,
            "parentCode": "000001003",
            "nodeCode": "000001003002",
            "name": "平陽縣",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ],
    [
        {
            "districtId": 1586533852834,
            "parentCode": "000",
            "nodeCode": "000001",
            "name": "浙江省",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873432,
            "parentCode": "000001",
            "nodeCode": "000001003",
            "name": "溫州市",
            "districtType": "HUADONG",
            "districtTypeName": null
        },
        {
            "districtId": 1586533873486,
            "parentCode": "000001003",
            "nodeCode": "000001003003",
            "name": "文成縣",
            "districtType": "HUADONG",
            "districtTypeName": null
        }
    ]
]
[
    {
        "label": "浙江省",
        "value": 1586533852834,
        "parentCode": "000",
        "nodeCode": "000001",
        "children": [
            {
                "label": "杭州市",
                "value": 1586533872922,
                "parentCode": "000001",
                "nodeCode": "000001001",
                "children": [
                    {
                        "label": "上城區",
                        "value": 1586533872940,
                        "parentCode": "000001001",
                        "nodeCode": "000001001001"
                    },
                    {
                        "label": "下城區",
                        "value": 1586533872961,
                        "parentCode": "000001001",
                        "nodeCode": "000001001002"
                    },
                    {
                        "label": "臨安區",
                        "value": 1586533872980,
                        "parentCode": "000001001",
                        "nodeCode": "000001001003"
                    }
                ]
            },
            {
                "label": "寧波市",
                "value": 1586533873196,
                "parentCode": "000001",
                "nodeCode": "000001002"
            },
            {
                "label": "溫州市",
                "value": 1586533873432,
                "parentCode": "000001",
                "nodeCode": "000001003",
                "children": [
                    {
                        "label": "平陽縣",
                        "value": 1586533873468,
                        "parentCode": "000001003",
                        "nodeCode": "000001003002"
                    },
                    {
                        "label": "文成縣",
                        "value": 1586533873486,
                        "parentCode": "000001003",
                        "nodeCode": "000001003003"
                    }
                ]
            }
        ]
    }
]

實現步驟

① 觀察輸入的資料結構為二維陣列,每個陣列中儲存了省市區的全部資料,此時首先需要將二維陣列一維化,以取得所有的節點資料,用於後續構建樹形結構。

let arr = [].concat.apply([], data)

② 得到所有需要構建樹形結構的資料後,發現陣列中存在重複資料

arrayToTree(data, rootNode) {
  let temp = {}
  let reduceArr = data.reduce((current, next) => {
    temp[next.districtId] ? "" : temp[next.districtId] = current.push(next)
    return current
  },[])
}

③ 資料規範化處理,把所有的資料處理成需要的節點資料

arrayToTree(data, rootNode) {
  let temp = {}
  let reduceArr = data.reduce((current, next) => {
    temp[next.districtId] ? "" : temp[next.districtId] = current.push(next)
    return current
  },[])
  // 2.陣列規範化
  let result = []
  reduceArr.forEach(item => {
    result.push({
      label:item.name,
      value:item.districtId,
      parentCode:item.parentCode,
      nodeCode:item.nodeCode,
      children: []
    })
  })
}

④ 採用遞迴的方式構建樹形結構

getTreeList(data, rootNode, list,{label, value,parentCode, nodeCode, children}) {
  // 構建父節點
  data.forEach(item =>{
    if (item.parentCode === rootNode) {
      list.push(item)
    }
  })

  list.forEach(item => {
    item.children = []
    // 構建子節點
    this.getTreeList(data, item.nodeCode, item.children,{label, value,parentCode, nodeCode, children});
    // 刪除空葉子節點
    if (item.children.length === 0) {
      delete item.children;
    }
  })
  return list;
}

完整程式碼

arrayToTree(data, rootNode, {label = 'label', value = 'value',parentCode = 'parentCode', nodeCode = 'nodeCode', children = ''} = {}) {
      // 1.陣列去重
      let temp = {}
      let reduceArr = data.reduce((current, next) => {
        temp[next.districtId] ? "" : temp[next.districtId] = current.push(next)
        return current
      },[])
      // 2.陣列規範化
      let result = []
      reduceArr.forEach(item => {
        result.push({
          label:item.name,
          value:item.districtId,
          parentCode:item.parentCode,
          nodeCode:item.nodeCode,
          children: []
        })
      })
      // 3.構建樹形結構
      return this.getTreeList(result,rootNode,[],{
        label,value,parentCode,nodeCode,children
      });
    },
    // 遞迴構建樹形結構
    getTreeList(data, rootNode, list,{label, value,parentCode, nodeCode, children}) {
      data.forEach(item =>{
        if (item.parentCode === rootNode) {
          list.push(item)
        }
      })

      list.forEach(item => {
        item.children = []
        this.getTreeList(data, item.nodeCode, item.children,{label, value,parentCode, nodeCode, children});
        if (item.children.length === 0) {
          delete item.children;
        }
      })
      return list;
    },

以上就是JS前端二維陣列生成樹形結構範例詳解的詳細內容,更多關於JS二維陣列樹形結構的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com