Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript递归(树结构)_Javascript - Fatal编程技术网

Javascript递归(树结构)

Javascript递归(树结构),javascript,Javascript,我有以下json: var jsonObj = [ { "parentIndex": '0' , "childIndex": '3' , "parent": "ROOT", "child": "root3" }, {

我有以下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" }
            }
        ]
    }
};
使用以下代码:

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 jsonObj=[
{
“父索引”:“0”,
“儿童索引”:“3”,
“父”:“根”,
“child”:“root3”
},
{
“父索引”:“3”,
“儿童索引”:“2”,
“父项”:“root3”,
“child”:“root2”
},
{
“父索引”:“3”,
“儿童索引”:“1”,
“父项”:“root3”,
“child”:“root1”
}
];
函数过滤器(数组、条件){
var结果=[];
对于(var i=0;i
您可以使用带有临时对象的单循环方法来收集所有节点和结果对象,该对象返回作为根节点的节点

var data=[{parentIndex:'0',childIndex:'3',parent:'ROOT',childIndex:'3',childIndex:'2',parent:'root3',childIndex:'3',childIndex:'1',parent:'root3',child:'root1}],
树=函数(数据,根){
var r,o={};
data.forEach(函数(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==根){
r=o[a.childIndex];
}否则{
o[a.parentIndex]=o[a.parentIndex]|{};
o[a.parentIndex]。children=o[a.parentIndex]。children | |[];
o[a.parentIndex].children.push(o[a.childIndex]);
}
});
返回r;
}(数据“0”);
控制台日志(树)

.as控制台包装{max height:100%!important;top:0;}
数据是否已排序?您尝试过什么吗?数据不需要排序(输出中也不需要排序)。这只是一个示例数据。为什么需要递归?