繁体   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