Search code examples
javascriptjavascript-objects

How to modify existing json structure by merging it with new one


Here i have existing structure like

{
    "name": "Parent",
    "hierarchyNumber": "0",
    "children": [ {
            "name": "ParentC1",
            "hierarchyNumber": "1",
            "children": [ {
                "name": "ParentC1C",
                "hierarchyNumber": "1.1",
                "children": []
            } ]
        }
    ]
}

And i have new structure like:

{
    "name": "Child",
    "hierarchyNumber": "0",
    "children": [ {
            "name": "ChildC1",
            "hierarchyNumber": "1",
            "children": [ {
                "name": "ChildCC1",
                "hierarchyNumber": "1.1",
                "children": []
            } ]
        }
    ]
}

I want to add new structure as child in "ParentC1C" and modify the existing structure hierarchy which should be in order with existing structure like below:

{
    "name": "Parent",
    "hierarchyNumber": "0",
    "children": [ {
        "name": "ParentC1",
        "hierarchyNumber": "1",
        "children": [ {
            "name": "ParentC1C",
            "hierarchyNumber": "1.1",
            "children": [ {
                "name": "Child",
                "hierarchyNumber": "1.1.1",
                "children": [ {
                    "name": "ChildC1",
                    "hierarchyNumber": "1.1.1.1",
                    "children": [ {
                        "name": "ChildCC1",
                        "hierarchyNumber": "1.1.1.1.1",
                        "children": []
                    } ]
                } ]
            } ]
        } ]
    } ]
}

Here i am able to traverse through tree and get the parent element from existing structure and add new structure there with code:

function traverseStructure( root, selectedParentElement ) {
    if( root.children && root.children.length > 0 ) {
        for( var k in root.children ) {
            if( root.children[ k ].Id === selectedParentElement ) {
                return root.children[ k ];
            } else if( root.children.length ) {
                return traverseStructure( root.children[ k ], selectedParentElement );
            }
        }
    }
}

But not able to understand how can i modify Hierarchical level of new structure as expected. Could anyone please help me here.


Solution

  • A bit strange question is there any Id you are checking ??
    Made a demo fixing hierarchyNumber while traversing, but child is placed @end.

    Anyway there is uncomplete info/definition in question to answer exactly.

    function put2lastEmpty(root, child) {
        var hierarchyNumber = root.hierarchyNumber;
        while(true) {
            root = root.children[0]; // in case there is just 1 ??
            if (hierarchyNumber == "0") hierarchyNumber = root.hierarchyNumber;
            else {
                hierarchyNumber += ".1";
                if(root) root.hierarchyNumber = hierarchyNumber
                else break;
            }
            if (!root.children.length && child) {
                root.children.push(child); // add child here
                child = null;
            }
        }
    }
    var root = root();
    put2lastEmpty(root, child())
    console.log(JSON.stringify(root,null,2))
    
    function root() { return {
        "name": "Parent",
        "hierarchyNumber": "0",
        "children": [ {
                "name": "ParentC1",
                "hierarchyNumber": "1",
                "children": [ {
                    "name": "ParentC1C",
                    "hierarchyNumber": "1.1",
                    "children": []
                } ]
            }
        ]
    };}
    
    function child() { return {
        "name": "Child",
        "hierarchyNumber": "0",
        "children": [ {
                "name": "ChildC1",
                "hierarchyNumber": "1",
                "children": [ {
                    "name": "ChildCC1",
                    "hierarchyNumber": "1.1",
                    "children": []
                } ]
            }
        ]
    };}