0

I came across a very strange problem in a recursive function. my code is as below:

tree_generator(startNode, dictionary) {
      let resultNode = startNode
      // find node name from dictionary
      let hasChild = dictionary[resultNode.name]
        ? dictionary[resultNode.name].parts.length > 0
          ? true
          : false
        : false
      if (hasChild) {
        //create child node object
        let children = _.map(dictionary[resultNode.name].parts, item => {
          let childpart = {
            id: 'd_' + item.name,
            name: item.name,
            children: [],
            ontology_name: item.partIri.replace(/.*otl\//, '') // need to update to related uri value
          }
          this.level = this.level + 1
          // recurve to get the result for child node
          this.tree_generator(childpart, dictionary)
          return childpart
        })
        console.log('real child')
        console.log(children)
        resultNode.children = children
        console.log('real result node in tree')
        console.log(resultNode)
      } else {
        //it is a leaf, delete child node
        delete resultNode.children
      }
      return resultNode
    }

When I pass a different dictionary parameter, the return result should be different, but actually, it always returns the same result with the last dictionary values.

When I console log the children and resultNode value, the children value is always right with the right dictionary, but the resultNode always assigns a different value than the real children.

Does anyone have any idea what did I do wrong here?

Testing data group: group1: startNode:

{
      id: '0_d_TestOne',

      name: 'TestOne',

      children: [],

      ontology_name: 'testOne'
    }

dictionary:

{
      TestOne: { uri: 'TestOne', parts: [{ name: 'testTwee', partIri: 'http://otl/testTee' }] },
      testTwee: { uri: 'testTwee', parts: [{ name: 'testDrie', partIri: 'http://otl/testDrie' }] },
      testDrie: { uri: 'testDrie', parts: [{ name: 'testVier', partIri: 'http://otl/testVier' }] },
      testVier: { uri: 'testVier', parts: [{ name: 'TestVijf', partIri: 'http://otl/TestVijf' }] }
    }

group2: startNode:

{
      id: '0_d_TestOne',

      name: 'TestOne',

      children: [],

      ontology_name: 'testOne'
    }

dictionary:

{
      TestOne: { uri: 'TestOne', parts: [{ name: 'TestTwo', partIri: 'http://otl/TestTwo' }] },
      TestTwo: { uri: 'TestTwo', parts: [{ name: 'TestThree', partIri: 'http://otl/TestThree' }] },
      TestThree: {
        uri: 'TestThree',
        parts: [{ name: 'TestFour', partIri: 'http://otl/TestFour' }]
      },
      TestFour: { uri: 'TestFour', parts: [{ name: 'TestFive', partIri: 'http://otl/TestFive' }] }
    }

the start node is the same, the dictionary is different, the expected tree output should be different

6
  • 1
    I don't see you awaiting anything so why is that function async? It would help if you could provide some additional information like the parameters you're passing into the function, and your expected result so we could help debug it. Commented Jan 19, 2022 at 12:13
  • do you have some data and wanted result of it? Commented Jan 19, 2022 at 12:13
  • Yes, I have added the testing data, please check on the bottom of the post Commented Jan 19, 2022 at 12:52
  • 1
    Why is your function declared as async? There's nothing asynchronous in your code. Commented Jan 19, 2022 at 13:06
  • What is this.level? Commented Jan 19, 2022 at 13:10

1 Answer 1

1

the start node is the same, the expected tree output should be different

There's your problem: your function doesn't create a new output, it does modify the startNode that you pass in. If you pass in the same object twice with different dictionaries, only the results from the last call will be stored in your object.

I would suggest that instead of passing in an object, you only pass in the name of the tree node that you want to get from the dictionary, and you always create a new result object.

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.