Javascript递归(树结构)

时间:2017-04-30 19:28:27

标签: javascript

我有以下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 中的递归将上面的json转换为树结构。树结构看起来像:

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

2 个答案:

答案 0 :(得分:0)

使用以下代码:

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);

答案 1 :(得分:0)

您可以使用带有临时对象的单循环方法来收集所有节点和结果对象,并返回作为根节点的节点。

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; }