0

I am trying to write a recursive function to parse a JSON object where the structure of my JSON object is like this

const dataToParse = {
    key: "someKey",
    someArbData: "",
    children: [
        {
            key: "someKey1",
            someArbData: "",
            children: [
                {
                key: "someKey5",
                ....
                },
                {
                    key: "someKey6"
                }
            ]
        },
        {
            key: "someKey2",
            someArbData: "",
            children: [
                {
                key: "someKey3",
                ....
                },
                {
                    key: "someKey4"
                }
            ]
        }
    ]
}

Basically I have a list where there's nested layers of children as shown above.

My goal is to parse this unreadable JSON object to a map in javascript where it would look like:

const parsedMap = {
    "someKey": {
        someArbData: "",
        children: [
            {
                key: "someKey1",
                someArbData: "",
                children: [
                    {
                    key: "someKey5",
                    ....
                    },
                    {
                        key: "someKey6"
                    }
                ]
            },
            {
                key: "someKey2",
                someArbData: "",
                children: [
                    {
                    key: "someKey3",
                    ....
                    },
                    {
                        key: "someKey4"
                    }
                ]
            }
        ]
    },
    "someKey1": {
        someArbData: "",
        children: [
            {
            key: "someKey5",
            ....
            },
            {
                key: "someKey6"
            }
        ]
    },
    "someKey2": {
        someArbData: "",
        children: [
            {
            key: "someKey3",
            ....
            },
            {
                key: "someKey4"
            }
        ]
    }
}

I was initially going to do a loop but the nesting level cannot be determined ahead of time. So, I was thinking of writing a recursive function in javascript.

5
  • 1
    What’s unreadable about it? Why would you only transform the parent object then add other objects at the parent level? Do you just want a map of keys to their associated objects? Commented Dec 20, 2022 at 22:42
  • When you say hashMap/map: are you actually wanting a Map, or just a JavaScript Object like the example? Commented Dec 20, 2022 at 23:07
  • @RandyCasburn I thought that’s what I said last. Commented Dec 20, 2022 at 23:08
  • @DaveNewton - yeah, reading back I guess you did :-) Commented Dec 20, 2022 at 23:09
  • Its around 10,000 lines which is what's unreadable. I just want to transform the data to a Map Commented Dec 21, 2022 at 0:39

1 Answer 1

1

The recursion should be fairly simple. Something like this:

const data = {
  key: 1,
  children: [
    { key: 2, children: [] },
    { key: 3, children: [] }
  ]
};

const itemsByKey = {};

addItemsRecursively(itemsByKey, data, i => i.key, i => i.children);

console.log(itemsByKey);

function addItemsRecursively(itemMap, node, getKey, getChildren)
{
  itemMap[getKey(node)] = node;
  for(let child of getChildren(node)) {
    addItemsRecursively(itemMap, child, getKey, getChildren);
  }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.