Here is my data structure:
{ projects: [
{ revisions: [
{ files: [] },
],
}
],
user: {}
}
I'm tyring to recursively create a JSON hierarchy structure of an object's parents and their ids. Check your console. Look at projects[0].revisions[0]._idTree, which contains the projects._id and revisions as a child of projects, just as expected. Now look at projects[0].revisions[0].files[0]._idTree, which contains projects.files as a sibling of projects.revisions, when files should be a child of projects.revisions. How do I fix this?
解决方案
Please rewrite recurseTree like this:
function recurseTree(tree, newKey, newId) {
if(angular.element.isEmptyObject(tree)) {
tree[newKey] = {_id: newId};
return;
}
var child = null; // find current tree's child
for(var key in tree) {
if (key != '_id') {
child = tree[key]; // found a child
break;
}
}
if (child) { // recursively process on child
recurseTree(child, newKey, newId);
} else { // no child, so just fill the tree
tree[newKey] = {_id: newId};
}
}