Reputation: 739
I currently have a nested javascript object of unknown depth. The object is generated purely dynamically, so I don't know the names of the parents of the property I want to delete ( I could rework some stuff to get them If I Have To, but I'm trying to avoid that).
I currently have a function called search_tree(name)
that searches through all the properties of the object until it finds a property name : "name"
, and then returns that object for adding data at that location of the tree.
However, I now need to delete that object from the tree, and have not yet gotten it to work. I have tried:
obj = search_tree(name);
delete obj;
and that doesn't work, I'm assuming because obj isn't actually the object in the tree, but a reference to it?
delete search_tree(name);
also yielded no results. Can this be done in this way, or will I need to alter the search_tree
function to somehow return the heritage as well (or just make a different one)? Thanks
The code from search_tree
function search_tree(element, matchingName){
if(element.name == matchingName){
return element;
}else if (element.children != null){
var result = null;
for(var child in element.children){
result = searchTree(element.children[child], matchingName);
}
return result;
} else {
return null;
}
}
Just realized this function may be a bit unclear without explanation. Each object in the tree has a child object called "children," in which any number of other objects will be stored. I added the extra "children" layer because the objects often have child objects that I do not want to be searched through as part of the tree. Element is the object being searched through
Upvotes: 1
Views: 3767
Reputation: 26696
Are you looking to remove the object from the tree? And that's all you're looking to do?
If so, then store the "parent" node of the tree inside of your search.
Write a second function -- maybe prune_tree
, where you pass in the parent and the object (or an object with both as properties), and then do a for ... in search of parent. If parent[key] === object, delete parent[key];
You now have a full tree where that particular parent no longer contains that object (or you should).
Given that search_tree
should be recursive, give it one more parameter (parent
), which you feed it once for every level of depth you hit (each child will have the same parent). Be sure to account for the parent being the root (and thus not having a parent).
When you find the object you want to kill, return { object : objectNode, parent : parentNode };
Put that into your prune function. The reference to parentNode means that when you delete parentNode.objectNode, it will also be deleted from the tree (because it's just a reference, afterall).
EDIT:
Based on the above:
function prune_tree (parent, child) {
var key = "";
for (key in parent) { if (parent.hasOwnProperty(key) && parent[key] === child) {
delete parent[key];
}
}
function search_tree (name, element, parent) {
var key = "";
if (element.name === name) {
return prune_tree(parent, element);
} else if (!element.children) {
return null;
} else {
parent = element.children;
for (key in children) { if (children.hasOwnProperty(key) {
return search_tree(name, children[key], parent);
}}
}
}
I'm not 100% sure of what you're actually doing when you're recursing (like whether you're depending on a specific return value, or whatever... I don't even know if there are multiple objects which might have that same name, on different branches -- including the root node).
But something like what I've got there should recurse your tree.
It's setting parent
to element.children
(the place where the children are stored), and then looping through each object in children to call the function over again, passing in parent
to the next set. So element
is going to be a child element, and parent
is going to be the children
object which holds it.
If element.name
is an exact match to name
, then call the separate function prune_tree
, and pass it the saved parent
element, and the current child element
.
Inside of prune_tree
, just iterate through the keys of parent
, until you find the child element
you're looking for. Then delete
it off of the parent.
There shouldn't really be any surprises here, and this particular set of functions will likely keep on running until every single node on every single branch has been visited... ...so if you've got more than, say, 2000 nodes, you might want to consider breaking this up into chunks, or else it's going to break the call stack of some browsers. Assuming you've got only 1000 nodes or less, or you're only targeting browsers with bigger stacks, this should prune everything with the same name.
Again, this all comes down to whether this is your intended outcome, or if you're depending on getting return values, to do something with them, or if you just expect to fire this, pass it the root of a tree, and expect the function to purify the branches.
I'm also not sure whether you want to do something with the pruned objects, or if the point is just to have a tree clean from the tyranny of whatever is named "name".
Upvotes: 2