亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用于構建樹的Javascript遞歸函數

用于構建樹的Javascript遞歸函數

神不在的星期二 2021-12-12 15:36:33
我正在使用JavaScript和jQuery作為客戶端腳本。我對Recursive functions. 我有一個 JSON 數據,如下所示,我嘗試通過編寫遞歸函數使用下面的 JSON 數據制作樹結構,但我無法構建樹結構。所需輸出:var treeNode = {                    id: 101, // random                    text: object.name,                    icon: "fas fa-plus",                    subNode: {                        // id, text, icon and subNode of Children object                        // recursive data,  So on....                     }                };任何人都可以建議我或幫助我Recursive function根據上述 JSON 數據編寫 javascript 或 jQuery ,以便我可以構建樹結構。我知道我在尋求幫助,因為我對遞歸函數的了解較少。
查看完整描述

2 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

它可以是這樣的,使用 json 數據模型


<!doctype html>


<html>

  <head>

    <link rel="stylesheet" href="lib/style.css">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

  </head>


  <body>

    <div id="myDiv"></div>

  </body>

  <script>


var treeData={

   "Id":"10",

   "text":"Document Categories",

   "icon":"fas fa-plus",

   "subNode":

    [

      {

         "Id":"11",

         "text":"Pdf Documents",

         "icon":"fas fa-plus",

         "subNode":[

            {

               "Id":"31",

               "text":"Book Pdfs",

               "icon":"fas fa-plus",

                "subNode":[]

            },

            {

               "Id":"32",

               "text":"EPub",

               "icon":"fas fa-plus",

               "subNode":[

                  {

                     "Id":"20",

                     "text":"EBook Epubs1",

                     "icon":"fas fa-plus",

                     "subNode":[]

                  },

                  {

                     "Id":"30",

                     "text":"EBook Epubs2",

                     "icon":"fas fa-plus",

                     "subNode":[]

                  }

               ]

            }

         ]

      },

      {

         "Id":"33",

         "text":"Text Documents",

         "icon":"fas fa-plus",

         "subNode":[

            {

               "Id":"32",

               "text":"Book Text",

               "icon":"fas fa-plus",

                "subNode":[]

            },

            {

               "Id":"35",

               "text":"Automatic Text",

               "icon":"fas fa-plus",

               "subNode":[]

            }

         ]

      }

   ]

};


    var newTree = AddRecursive(null, treeData);

    var treeDiv = $('#myDiv');

    treeDiv.append(newTree);



function AddRecursive(tree, data) {

  if (tree == null) {

    tree = $('<ul/>');

    tree.attr('id', 'treeID');

  }


  var listU = $('<ul />');

  listU.addClass('ul class');


  var listItem = $('<li />');

  listItem.addClass('li class');

  listItem.attr('data-id', data.Id);


  var link = $('<a />');

  var i = $('<i/>').addClass('fa fa-folder');

  link.append(i);


  //link.addClass("linkClass");

  link.append(data.text);

  listItem.append(link);


  if (data.subNode.length > 0) {

    var span = $(' <span />');

    span.addClass('fa-chevron-down');

    link.append(span);

  }


  listU.append(listItem);

  tree.append(listU);


  for (i in data.subNode) {

    AddRecursive(listItem, data.subNode[i]);

  }

  return tree;

}




  </script>

</html>


查看完整回答
反對 回復 2021-12-12
?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

如果我們稍微抽象一下,就可以很容易地編寫一個通用的樹映射函數。然后我們可以提供兩個回調函數:一個查找輸入的子節點,一個根據輸入和映射的子節點構建輸出節點。事實證明,這樣的函數出奇的簡單:


const mapTree = (getChildren, transformNode) => (tree) =>

  transformNode (

    tree, 

    (getChildren (tree) || []) .map (mapTree (getChildren, transformNode))

  )

對于您的數據,getChildren簡直就是(node) => node._children


節點轉換可能很簡單:


const transformNode = (node, children) => 

  ({

    id: node.$id,         // or a randomizing call?

    text: node.name,

    icon: "fas fa-plus",  // is this really a fixed value?

    subNode: children

  })

把這些放在一起,我們得到


const mapTree = (getChildren, transformNode) => (tree) =>

  transformNode (

    tree, 

    (getChildren (tree) || []) .map (mapTree (getChildren, transformNode))

  )


const kids = (node) => node._children


const transformNode = (node, children) => 

  ({

    id: node.$id,

    text: node.name,

    icon: "fas fa-plus",    

    subNode: children

  })


const myTransform = mapTree (kids, transformNode)


const jsonData = { "$id": "45", "_children": [{ "$id": "46", "_children": [{ "$id": "47", "_children": [{ "$id": "48", "_children": [{ "$id": "49", "_children": null, "id": "Test1", "text": "Text1", "name": "name1", "parent": null, "root": { "$ref": "49" }, "depth": 0, "children": [] }], "id": "id1", "text": "text2", "name": "name2", "parent": null, "root": { "$ref": "48" }, "depth": 0, "children": [{ "$ref": "49" }] }], "id": "id3", "text": "text4", "name": "name4", "parent": null, "root": { "$ref": "47" }, "depth": 0, "children": [{ "$ref": "48" }] }, { "$id": "50", "_children": [{ "$id": "51", "_children": [{ "$id": "52", "_children": null, "id": "id6", "text": "text6", "name": "name6", "parent": null, "root": { "$ref": "52" }, "depth": 0, "children": [] }], "id": "id7", "text": "text7", "name": "name7", "parent": null, "root": { "$ref": "51" }, "depth": 0, "children": [{ "$ref": "52" }] }], "id": "id8", "text": "text8", "name": "name8", "parent": null, "root": { "$ref": "50" }, "depth": 0, "children": [{ "$ref": "51" }] }], "id": "id9", "text": "text9", "name": "name9", "parent": null, "root": { "$ref": "46" }, "depth": 0, "children": [{ "$ref": "47" }, { "$ref": "50" }] }, { "$id": "53", "_children": [{ "$id": "54", "_children": null, "id": "id10", "text": "text10", "name": "name10", "parent": null, "root": { "$ref": "54" }, "depth": 0, "children": [] }], "id": "id11", "text": "text11", "name": "name11", "parent": null, "root": { "$ref": "53" }, "depth": 0, "children": [{ "$ref": "54" }] }], "id": "0", "text": "0", "name": "", "parent": null, "root": { "$ref": "45" }, "depth": 0, "children": [{ "$ref": "46" }, { "$ref": "53" }] }


console .log (myTransform (jsonData))


這與您請求的輸出略有不同。你寫了subNode: { ... },但我返回了一個對象數組,subNodes: [ ... ],因為我在這里沒有任何真正意義上的普通對象。


此外,subNodes如果輸入節點沒有子節點,這將產生一個空數組。如果您不想擁有該subNodes財產,則可以更換


    subNode: children


    ...(children .length ? {subNode: children} : {})

顯然,您不需要命名的助手,并且可以mapTree使用如下匿名函數進行調用:


const myTransform = mapTree (

  (node) => node._children, 

  (node, children) => 

    ({

      id: node.$id,

      text: node.name,

      icon: "fas fa-plus",    

      subNode: children

    })

)

這個mapTree函數很容易編寫,因為我在編寫它時不必考慮輸出或輸入格式的任何細節。但也許這種抽象對我沒有幫助,除了這里我永遠不會使用它。如果是這樣,我可以通過直接插入硬編碼回調來簡單地重新設計抽象版本。只需稍加操作,即可將其變成這個版本:


const newTransform = (node) =>

  ({

    id: node.$id,

    text: node.name,

    icon: "fas fa-plus",    

    subNode: (node._children || []).map(newTransform)

  })


const jsonData = { "$id": "45", "_children": [{ "$id": "46", "_children": [{ "$id": "47", "_children": [{ "$id": "48", "_children": [{ "$id": "49", "_children": null, "id": "Test1", "text": "Text1", "name": "name1", "parent": null, "root": { "$ref": "49" }, "depth": 0, "children": [] }], "id": "id1", "text": "text2", "name": "name2", "parent": null, "root": { "$ref": "48" }, "depth": 0, "children": [{ "$ref": "49" }] }], "id": "id3", "text": "text4", "name": "name4", "parent": null, "root": { "$ref": "47" }, "depth": 0, "children": [{ "$ref": "48" }] }, { "$id": "50", "_children": [{ "$id": "51", "_children": [{ "$id": "52", "_children": null, "id": "id6", "text": "text6", "name": "name6", "parent": null, "root": { "$ref": "52" }, "depth": 0, "children": [] }], "id": "id7", "text": "text7", "name": "name7", "parent": null, "root": { "$ref": "51" }, "depth": 0, "children": [{ "$ref": "52" }] }], "id": "id8", "text": "text8", "name": "name8", "parent": null, "root": { "$ref": "50" }, "depth": 0, "children": [{ "$ref": "51" }] }], "id": "id9", "text": "text9", "name": "name9", "parent": null, "root": { "$ref": "46" }, "depth": 0, "children": [{ "$ref": "47" }, { "$ref": "50" }] }, { "$id": "53", "_children": [{ "$id": "54", "_children": null, "id": "id10", "text": "text10", "name": "name10", "parent": null, "root": { "$ref": "54" }, "depth": 0, "children": [] }], "id": "id11", "text": "text11", "name": "name11", "parent": null, "root": { "$ref": "53" }, "depth": 0, "children": [{ "$ref": "54" }] }], "id": "0", "text": "0", "name": "", "parent": null, "root": { "$ref": "45" }, "depth": 0, "children": [{ "$ref": "46" }, { "$ref": "53" }] }


console .log (newTransform (jsonData))


這里有一個重要的點。這個通用函數比我嘗試編寫一些東西來直接轉換格式要容易得多。雖然過早的抽象存在危險,但它也可以提供顯著的好處。我可能會選擇只保留最后一個版本,但通用抽象簡化了它的開發。


查看完整回答
反對 回復 2021-12-12
  • 2 回答
  • 0 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號