Help convert one type of javascript object to another type

I need to convert a JavaScript object of one type:

[
        {
            "label": "A",
            "children": [
                {
                    "label": "A.1",
                    "children": [
                        {
                            "label": "A.1.1",
                            "children": []
                        },
                        {
                            "label": "A.1.2",
                            "children": []
                        }
                    ]
                },
                {
                    "label": "A.2",
                    "children": [
                        {
                            "label": "A.2.1",
                            "children": []
                        },
                        {
                            "label": "A.2.2",
                            "children": []
                        }
                    ]
                }
            ]
        },
        {
            "label": "B",
            "children": []
        }
    ]

and I want to convert it to this type:

{
        "l1": {
            "item1": {
                "label": "A",
                "reference": "l2"
            },
            "item2": {
                "label": "B"
            }
        },
        "l2": {
            "item1" : {
                "label": "A.1",
                "reference": "l3"
            },
            "item2" : {
                "label": "A.2",
                "reference" : "l4"
            }
        },
        "l3" : {
            "item1" : {
                "label": "A.1.1"
            },
            "item2":{
                "label": "A.1.2"
            }
        },
        "l4" : {
            "item1" : {
                "label": "A.2.1"
            },
            "item2":{
                "label": "A.2.2"
            }
        }
    }

I have been trying this but failing somehow. Could you please give me some hint how can I do this conversion. Thanks.

What are the rules for converting? I don’t understand how exactly the second is made from the first, and that’s definitely the first part to writing a code to do this.

well the original object is like a hierarchy of items (like a graph) & I want this to be converted to sort of a level order traversal.

Right, but how does that conversion work? i.e. for X in the first object, it goes into the second object by…

Walk us through how you would manually do it with the example you posted.

2 Likes
  1. I would iterate over the outermost array containing A & B objects, & populate them as values for Level1 key.
    { "level1" : { "item1" : objectA, "item2": objectB } }
  2. I would check if object A has children,
    2.1. if it does, I would have a key-value pair like: "reference":"A's_child_identifier" added to item1’s value & would would want to do the above process again for children (which is an array of objects)
    2.2. this time the children of objectA will have a key-value pair as: "A's_child_identifier" : {"item1": A.child1, "item2":A.child2}
    2.3. this key-value will be appended after "level1":
{ "level1"  : { "item1" : {"label":"A", "reference": "level2"}, "item2": objectB },
"level2" : {"item1": A.child1, "item2":A.child2}
}
  1. and so on.

So the converted object will basically be like:

{ "level1"  : { "item1" : {"label":"A", "reference": "level2"}, "item2": {"label":"B", "reference": "level3"} },
"level2" : { "item1": {"label":"A.1"}, "item2":{"label":"A.2"} },
"level3": { <similar to level2, but having child elements of objectB (object with label B)>}
}

Here, the "reference": "level_x" indicates that this sub-object has child elements which are present in the main object with key as "level_x".

But why does your original desired final object have a l4? There is only a depth of 3 from what I can see. When you attempted to manually map it out you end of with a level1, level2, and level3 properties of the main object created. Which is the correct desired outcome from the original array?

Yes that’s right. Here only we have depth of 3. Sorry to add to the confusion. I should have used better naming than L1, L2.

Basically the reference field identifies that a node has child items. And the child items can be found in the key denoted by value of reference.
And since it’s sort of a tree structure, there can be multiple depths.

I’m sort of near the solution, just stuck on adding the (reference: level_x) or (reference: l_x) key-value pair.

let obj2 = {};

function operation(level, object) {

    let obji = {};
    for (let i = 0; i < object.length; ++i) {
        obji[`item${i}`] = object[i];
    }
    obj2[`level_${level}`] = obji;

    for (let i = 0; i < object.length; i++) {
        if(object[i].hasOwnProperty("children") && object[i].children.length) {
            operation(++level, object[i].children);
        }
    }
}

Completed the formulation:

var obj2 = {};
var level = 0;

function operation(object) {

    let obji = {};
    for (let i = 0; i < object.length; ++i) {
        obji[`item${i}`] = { 
            "id": object[i][id],
            "label": object[i]["label"],
        };
    }
    obj2[`level_${level}`] = obji;

    let level_x = "level_" + level;
    for (let i = 0; i < object.length; i++) {
        if(object[i].children !== undefined && object[i].children.length) {
            level = level+1;
            obj2[level_x][`item${i}`].reference = "level_"+level;
            operation(object[i].children);
        }
    }
}

This code errors out for me on the following line. You never define what id is.

            "id": object[i][id],

ah! My bad. Placed it by mistake. Thanks.
must remove that part