Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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
  ]
]

The 5th element in the arrays is my section depth so what I am trying to do is build a tree structure in javascript.But I don't know and understand how to do this. Here is where I am at right now as far as code goes:

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

My ancester_branch is to keep track of the nodes as I build the tree and push and pop from it. I don't where to go from here or what to do. So I hope some people can help me out thanks in advance for the help.

share|improve this question
    
I just don't know how to convert a flat array to a tree in javascript and any help would be great. –  Jamal Martin Jul 8 '13 at 4:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.