title: Javascript variable "jsonElement" is being replaced in recursion
body: <p>Hii have the recursion code below and while stepping through it i noticed that the value of jsonElement is not preserved well after stepping out of inner recursions. How do i go about it?</p>
  
  <pre><code>allnodes = getArrayOfNodes(tree);
  function toJson(node){
  
      childrenList = []
  
      jsonElement = {}
      jsonElement["name"] = node.data
  
  
      allnodes.forEach(function(item, index){
          if(isParentOf(node, item) == true){
              allnodes.splice(index, 1)
              childrenList.push(toJson(item))
          }
      })
      jsonElement["children"] = childrenList
  
      return jsonElement
  }
  </code></pre>