Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8617365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:50:22+00:00 2026-06-12T05:50:22+00:00

var treeData = {name : A, children : [ {name : B, children: [

  • 0
   var treeData = {"name" : "A",  "children" : [
                         {"name" : "B", "children": [
                                 {"name" : "C", "children" :[]}
                          ]}
                   ]};

THE ARRAY BEFORE SHOULD BE EMPTY. THE ARRAY AFTER SHOULD BE POPULATED DEPENDING ON THE NUMBER OF NODES NEEDED THAT WILL BE DEFINED FROM A DYNAMIC VALUE THAT IS PASSED.

I would like to build the hierarchy dynamically with each node created as a layer/level in the hierarchy having its own array of nodes. THIS SHOULD FORM A TREE STRUCTURE. This is hierarchy structure is described in the above code.There should be a root node, and an undefined number of nodes and levels to make up the hierarchy size. Nothing should be fixed besides the root node. I do not need to read or search the hierarchy, I need to construct it.
The array should start {“name” : “A”, “children” : []} and every new node as levels would be created {“name” : “A”, “children” : [HERE-{“name” : “A”, “children” : []}]}. In the child array, going deeper and deeper. Basically the array should have no values before the call, except maybe the root node. After the function call, the array should comprise of the required nodes of a number that may vary with every call depending on the results of a database query. Every child array will contain one or more node values. There should be a minimum of 2 node levels, including the root.
It should initially be a Blank canvas, that is no predefined array values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:50:24+00:00Added an answer on June 12, 2026 at 5:50 am

    Here is the example of function to create your nodes dynamically:

    function createNode(name) {
       return({name: name, children: []});
    }
    
    function addChild(node, child) {
        node.children.push(child);
        return node;
    }
    
    var treeData = createNode("");
    var subChild = createNode("");
    addChild(subChild, createNode("A31"));
    addChild(treeData, subChild);
    

    But I suggest to use prototypes instead.

    To find any node by ‘path’ with any level:

    function findNodeByPath(root, path) {
       var curr; 
       while(root && ((curr = path.splice(0,1)[0]) !== undefined)) {
            if (root.children) root = root.children[curr];
            else root = undefined;
       }
       return root;
    }
    
    
    function findNodeByName(root, namePath, create) {
       var curr; 
       while(root && ((curr = namePath.splice(0,1)[0]) !== undefined)) {
            if (root.children) {
                var found = undefined;
                for (var i = 0; !found && i < root.children.length; i++)
                    if (root.children[i].name == curr) found = root.children[i];
                if (create && !found) {
                    found = createNode(curr);
                    addChild(root, found);
                }
                root = found;
            }
            else root = undefined;
       }
       return root;
    }
    
    
    var A31 = findNodeByPath(treeData, [0, 0]); // Will return the node with name A31
    addChild(A31, createNode("A31 child 1"));
    addChild(A31, createNode("A31 child 2"));
    
    // second child will be accessible by:
    var secondChildOfA31 = findNodeByPath(treeData, [0, 0, 1]);
    
    // also to find node by the name:
    var secondChildOfA31 = findNodeByName(treeData, ["", "A31", "A31 child 2"]);
    
    // will create all intermenient nodes with respective names:
    var veryDeepChild =  findNodeByName(treeData, ["foo", "bar", "baz", "quux", "moo"], true);
    
    function createOuterNode(name, childNode) {
        return {name: name, children: childNode? [childNode] : []}
    }
    
    // Example to create nodes in the question:
    var CNode = createOuterNode("C");
    var BNode = createOuterNode("B", CNode);
    var ANode = createOuterNode("A", BNode);
    
    // Example using LOOP:
    var list = ["A", "B", "C"];
    var outer = undefined;
    for (var i = list.length - 1; i >= 0; i--) outer = createOuterNode(list[i], outer);
    
    // outer will contain A node with child B with child C
    console.log(outer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var fruit = [apple,pear,pear,pear,banana]; How do I remove all pear fruit from this array?
var conversations = new Array(); jQuery('.CChatWindow').each(function(){ if (jQuery(this).is(:visible) && jQuery(this).attr(data-conversationid) != 0) { alert(jQuery(this).attr(data-conversationid));
var chart; var d = new Array(); function click() { d = document.getElementById('graph:hi').value; alert(d);
I'm trying to use dynatree, and it can be initialized like this: var treeData
I've found that I can't delete nodes that haven't been rendered yet. The following
var $imagefile = $('<input />') .attr({ type: 'file', name: 'imageFile', id: 'imageFile' }); Above
var number = 342345820139586830203845861938475676 var output = [] var sum = 0; while (number)
var array = [3,9,23,76,1,54,21,12,0,9,2]; var shuffled = array.sort(function() {return 0.5 - Math.random()}); console.log(shuffled); I'm
var greentext='shopping'; var colors=new Array('green','blue','red','violet','brown'); for(i=0;i<colors.length;i++) { //suppose consider colors[i] is green } When
var $content = $('#SomeDivContainingTwoImages'); $content.children().each(function(i){ $(this).showImage = showImageStatic; $(this).showImage(); }); returns Uncaught TypeError: Object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.