将平面数组转换为树javascript

时间:2013-07-07 21:47:22

标签: arrays tree flat

So I have this flat array which has three arrays:
[
  [
    "a198f9db5e814b11b6e8b885b7978250",
    "New Section",
    0,
    0,
    0,
    0
  ],
  [
    "F0CA2865AA708377EE73A64F98B04BE0",
    "New Section",
    0,
    0,
    2,
    1
  ],
  [
    "B3BADFC6F5A5ECAC1619A651A33C2692",
    "New Section",
    0,
    0,
    14,
    2
  ]
]

数组中的第5个元素是我的部分深度,所以我要做的是在javascript中构建树结构。但我不知道并理解如何执行此操作。就代码而言,这就是我现在所处的位置:

    var input = flatArray;
                var root = {nodes:[], depth: -1};
                var ancester_branch = [root];
                var current_target = root;


                for(var i = 0; i < input.length; i++){
                    var node = input[i];
                    while(node.depth <= current_target.depth){
                        ancester_branch.pop();
                        current_target = ancester_branch[-1];
                    }

                    current_target.nodes.push(node);
                    current_target = node;
                    ancester_branch.push(node);
                    return root;
                };
my  var input = rToc.metadata[0].sections;
            var root = {nodes:[], depth: -1};
            var ancester_branch = [root];
            var current_target = root;


            for(var i = 0; i < input.length; i++){
                var node = input[i];
                while(node.depth <= current_target.depth){
                    ancester_branch.pop();
                    current_target = ancester_branch[-1];
                }

                current_target.nodes.push(node);
                current_target = node;
                ancester_branch.push(node);
                return root;
            };
 var input = rToc.metadata[0].sections;
            var root = {nodes:[], depth: -1};
            var ancester_branch = [root];
            var current_target = root;


            for(var i = 0; i < input.length; i++){
                var node = input[i];
                while(node.depth <= current_target.depth){
                    ancester_branch.pop();
                    current_target = ancester_branch[-1];
                }

                current_target.nodes.push(node);
                current_target = node;
                ancester_branch.push(node);
                return root;
            };
 var input = rToc.metadata[0].sections;
            var root = {nodes:[], depth: -1};
            var ancester_branch = [root];
            var current_target = root;


            for(var i = 0; i < input.length; i++){
                var node = input[i];
                while(node.depth <= current_target.depth){
                    ancester_branch.pop();
                    current_target = ancester_branch[-1];
                }

                current_target.nodes.push(node);
                current_target = node;
                ancester_branch.push(node);
                return root;
            };
 var input = rToc.metadata[0].sections;
            var root = {nodes:[], depth: -1};
            var ancester_branch = [root];
            var current_target = root;


            for(var i = 0; i < input.length; i++){
                var node = input[i];
                while(node.depth <= current_target.depth){
                    ancester_branch.pop();
                    current_target = ancester_branch[-1];
                }

                current_target.nodes.push(node);
                current_target = node;
                ancester_branch.push(node);
                return root;
            };

我的ancester_branch是在构建树并从中推送和弹出时跟踪节点。我不会从这里去或做什么。所以我希望有些人可以提前帮助我,谢谢你的帮助。

0 个答案:

没有答案