簡體   English   中英

Javascript遞歸(樹結構)

[英]Javascript recursion (Tree- Structure)

我有以下json:

var jsonObj = [
              {
                       "parentIndex": '0'  ,
                       "childIndex": '3'  ,
                       "parent": "ROOT",
                       "child": "root3"
               },
               {
                      "parentIndex": '3'  ,
                      "childIndex": '2'  ,
                      "parent": "root3" ,
                      "child": "root2"
               },
               {
                       "parentIndex": '3'  ,
                       "childIndex": '1'  ,
                       "parent": "root3" ,
                       "child": "root1"
               }
               ];

我需要在Javascript中使用Recursion將上面的json轉換為Tree-structure。 樹結構看起來像:

nodeStructure: {
        text: { name: "root3" },
        children: [
            {
                text: { name: "root2" }
            },
            {
                text: { name: "root1" }
            }
        ]
    }
};

使用以下代碼:

var jsonObj = [
          {
                   "parentIndex": '0'  ,
                   "childIndex": '3'  ,
                   "parent": "ROOT",
                   "child": "root3"
           },
           {
                  "parentIndex": '3'  ,
                  "childIndex": '2'  ,
                  "parent": "root3" ,
                  "child": "root2"
           },
           {
                   "parentIndex": '3'  ,
                   "childIndex": '1'  ,
                   "parent": "root3" ,
                   "child": "root1"
           }
           ];

function filter(array,condition){
  var result = [];
  for (var i = 0; i < array.length; i++) {
    if(condition(array[i])){
      result.push(array[i]);
    }
  }
  return result;
}


function getChilds(parentKey,items){
  var subItems =  filter(items,function(n){return n.parent === parentKey});
  var result = [];
  for (var i = 0; i < subItems.length; i++) {
    var subItem = subItems[i];
    var resultItem = {
      text: {name:subItem.child}
    };
    var kids = getChilds(subItem.child , items);
    if(kids.length){
      resultItem.children = kids;
    }
    result.push(resultItem);
  }
  return result;
}


var rootItems = getChilds('ROOT',jsonObj);

您可以使用帶有臨時對象的單循環方法來收集所有節點和結果對象,並返回作為根節點的節點。

 var data = [{ parentIndex: '0', childIndex: '3', parent: "ROOT", child: "root3" }, { parentIndex: '3', childIndex: '2', parent: "root3", child: "root2" }, { parentIndex: '3', childIndex: '1', parent: "root3", child: "root1" }], tree = function (data, root) { var r, o = {}; data.forEach(function (a) { o[a.childIndex] = { text: { name: a.child } }; if (o[a.childIndex] && o[a.childIndex].children) { o[a.childIndex].children = o[a.childIndex].children; } if (a.parentIndex === root) { r = o[a.childIndex]; } else { o[a.parentIndex] = o[a.parentIndex] || {}; o[a.parentIndex].children = o[a.parentIndex].children || []; o[a.parentIndex].children.push(o[a.childIndex]); } }); return r; }(data, '0'); console.log(tree); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM