1
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 は、ツリーを構築し、そこからプッシュおよびポップするときにノードを追跡することです。ここからどこへ行くべきか、何をすべきかわかりません。ですから、事前に助けてくれてありがとう、何人かの人々が私を助けてくれることを願っています。

4

0 に答える 0