Recursive function in JS

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


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.