Hii下面有递归代码,在单步执行时,我注意到在退出内部递归后,jsonElement的值不能很好地保留。我该怎么做呢?
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
}