Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8520593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:34:42+00:00 2026-06-11T06:34:42+00:00

I currently have a nested javascript object of unknown depth. The object is generated

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:34:44+00:00Added an answer on June 11, 2026 at 6:34 am

    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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi currently I have a nested XMl , having the following Structure : <?xml
I currently have a table with a nested table in it: <table class=datagrid id=report>
How would I create a nested list, I currently have this public function getNav($cat,$subcat){
I have about 200 categories that are nested. I am currently reading the documention
I have a #wrapper div and a #grid div nested inside. currently I can
I am currently working on rails3 application that uses jQuery. I have a javascript
I currently have 2 JavaScript variables in which I need to retrieve values from.
I currently have many linq expressions nested within foreach loops ... kind of defeating
I have 2 javascript loops, one nested in the other, that does an AJAX
I'm struggling on how to correctly track nested repositories using TortoiseHg. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.