js遞歸生成樹形結構-vue


{
"code": "0",
"message": "SUCCESS",
"data": [
{
"code": 1,
"parentCode": 0,
"value": "IPO上市",
"status": 0,
"comment": "IPO上市",
"showOrder": 1
},
{
"code": 2,
"parentCode": 0,
"value": "公司制改建",
"status": 0,
"comment": "公司制改建",
"showOrder": 2
},
{
"code": 3,
"parentCode": 0,
"value": "對外投資",
"status": 0,
"comment": "對外投資",
"showOrder": 3
},
{
"code": 4,
"parentCode": 0,
"value": "接受投資",
"status": 0,
"comment": "接受投資",
"showOrder": 4
},
{
"code": 5,
"parentCode": 0,
"value": "合並、分立、破產、清算、解散",
"status": 0,
"comment": "合並、分立、破產、清算、解散",
"showOrder": 5
},
{
"code": 6,
"parentCode": 0,
"value": "股東股權比例變動",
"status": 0,
"comment": "股東股權比例變動",
"showOrder": 6
},
{
"code": 7,
"parentCode": 0,
"value": "產權轉讓",
"status": 0,
"comment": "產權轉讓",
"showOrder": 7
},
{
"code": 8,
"parentCode": 0,
"value": "上市公司發行股份購買資產",
"status": 0,
"comment": "上市公司發行股份購買資產",
"showOrder": 8
},
{
"code": 9,
"parentCode": 0,
"value": "資產轉讓、置換、拍賣",
"status": 0,
"comment": "資產轉讓、置換、拍賣",
"showOrder": 9
},
{
"code": 10,
"parentCode": 0,
"value": "資產抵押/質押",
"status": 0,
"comment": "資產抵押/質押",
"showOrder": 10
},
{
"code": 11,
"parentCode": 0,
"value": "資產捐贈",
"status": 0,
"comment": "資產捐贈",
"showOrder": 11
},
{
"code": 12,
"parentCode": 0,
"value": "資產租賃",
"status": 0,
"comment": "資產租賃",
"showOrder": 12
},
{
"code": 13,
"parentCode": 0,
"value": "資產補償/損失補償",
"status": 0,
"comment": "資產補償/損失補償",
"showOrder": 13
},
{
"code": 14,
"parentCode": 0,
"value": "資產償債",
"status": 0,
"comment": "資產償債",
"showOrder": 14
},
{
"code": 15,
"parentCode": 0,
"value": "資產涉訟",
"status": 0,
"comment": "資產涉訟",
"showOrder": 15
},
{
"code": 16,
"parentCode": 0,
"value": "資產收購",
"status": 0,
"comment": "資產收購",
"showOrder": 16
},
{
"code": 17,
"parentCode": 0,
"value": "接受抵債資產",
"status": 0,
"comment": "接受抵債資產",
"showOrder": 17
},
{
"code": 18,
"parentCode": 0,
"value": "債轉股",
"status": 0,
"comment": "債轉股",
"showOrder": 18
},
{
"code": 19,
"parentCode": 0,
"value": "債務重組及其他",
"status": 0,
"comment": "債務重組及其他",
"showOrder": 19
},
{
"code": 20,
"parentCode": 0,
"value": "追溯評估",
"status": 0,
"comment": "追溯評估",
"showOrder": 20
},
{
"code": 21,
"parentCode": 0,
"value": "復核報告",
"status": 0,
"comment": "復核報告",
"showOrder": 21
},
{
"code": 22,
"parentCode": 0,
"value": "了解價值",
"status": 0,
"comment": "了解價值",
"showOrder": 22
},
{
"code": 23,
"parentCode": 0,
"value": "財務會計報告目的",
"status": 0,
"comment": "財務會計報告目的",
"showOrder": 23
},
{
"code": 24,
"parentCode": 0,
"value": "計稅價格評估",
"status": 0,
"comment": "計稅價格評估",
"showOrder": 24
},
{
"code": 25,
"parentCode": 0,
"value": "認定報關價格",
"status": 0,
"comment": "認定報關價格",
"showOrder": 25
},
{
"code": 26,
"parentCode": 0,
"value": "其他",
"status": 0,
"comment": "其他",
"showOrder": 26
}
]
}

  

// 生成樹結構
    initTree (parentCode) {
      // jsonArray 變量數據
      // 第一次以后:根據id去查詢parent_id相同的(相同為子數據)
      // 第一次:查找所有parent_id為-1的數據組成第一級
      let that = this
      let res = that.dataList // 你的樹結構

      const child = res.filter(item => item.parentCode == parentCode)
      // 第一次:循環parent_id為-1數組
      return child.map(item => ({
          ...item,
          // 當前存在id(id與parent_id應該是必須有的)調用initTree() 查找所有parent_id為本id的數據
          // childs字段寫入
          childs: that.initTree(item.code)
      }))
    },

  

this.initTree(0)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM