Home > database >  How to write a recursive function to create a hashMap in javascript?
How to write a recursive function to create a hashMap in javascript?

Time:12-21

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.

CodePudding user response:

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

  • Related