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 461625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:59:36+00:00 2026-05-12T22:59:36+00:00

I have a problem with dereferencing a Javascript object and setting it to NULL.

  • 0

I have a problem with dereferencing a Javascript object and setting it to NULL.

Here, I have a Folder implementation that supports recursive subdirectory removal. Please see my comments to understand my dilemma.

function Folder(name, DOM_rows) {
    this.name = name;
    this.files = [].concat(DOM_rows);
    this.subdirs = [];
}

Folder.prototype.AddDir(name, DOM_rows) {
   this.subdirs.push(new Folder(name, DOM_rows));
}

Folder.prototype.RemoveDir(folder) {
   var stack = [folder];
   while(stack.length > 0) {
      var cur = stack.pop();
      // do a post-order depth-first traversal, so dig to the deepest subdir:
      if(cur.subdirs.length > 0) {
          while(cur.subdirs.length > 0) { stack.push(cur.subdirs.pop()); }
      } else {
          // arrived at a leaf-level:
          cur.files = null;
          // now how do I delete cur from it's parent's subdirs array?
          // the only way I know how is to keep a "cur.parentDir" reference,
          // then find parent.subdirs[ index of cur ] and slice it out.
          // How can I do the JS-equivalent of *cur = NULL?
      }
   }
}
  • 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-05-12T22:59:36+00:00Added an answer on May 12, 2026 at 10:59 pm

    Note that you don’t have as big a problem as you suspect, since all subdirectories but folder in your RemoveDir will be deleted from their parent’s subdir by the stack.push(cur.subdirs.pop()); line

    To find a subdirectory in a parent, you could make use an object-as-dictionary rather than an array for subdirs:

    function Folder(name, DOM_rows, parent) {
        this.name = name;
        this.parent = parent;
        this.files = [].concat(DOM_rows);
        this.subdirs = {};
        this.subdirCount = 0;
    }
    
    Folder.prototype.AddDir = function (name, DOM_rows) {
        if (this.subdirs[name]) {
            return null;
        }
        ++this.subdirCount;
        return this.subdirs[name] = new Folder(name, DOM_rows, this);
    }
    

    Given a folder, you can remove the folder from the parent with:

    delete folder.parent.subdirs[folder.name];
    

    Here’s a preorder version:

    Folder.prototype.RemoveDir = function (folder) {
      if (this.subdirs[folder.name] === folder) {
          var stack = [folder];
          while(stack.length > 0) {
              var cur = stack.pop();
              // pre-order
              delete cur.files;
              // if there's other processing to be done, now's the time to do it
              for (subdir in cur.subdirs) {
                  stack.push(cur.subdirs[subdir]);
                  delete cur.subdirs[subdir];
              }
              // it's unnecessary to set subdir count, since 'cur' has been deleted
              //cur.subdirCount = 0;
          }
          delete this.subdirs[folder.name];
          --this.subdirCount;
      }
    }
    

    And the recursive post-order version:

    Folder.prototype.RemoveChildren = function () {
        for (subdir in this.subdirs) {
            this.RemoveDir(this.subdirs[subdir]);
        }
    }
    
    Folder.prototype.RemoveDir = function (folder) {
        if (this.subdirs[folder.name] === folder) {
            folder.RemoveChildren();
            folder.files = [];
            delete this.subdirs[folder.name];
            --this.subdirCount;
        }
    }
    

    And the iterative post-order version:

    Array.prototype.top = function () { return this[this.length-1]; }
    
    Folder.prototype.RemoveDir = function (folder) {
      if (this.subdirs[folder.name] === folder) {
          var stack = [folder];
          while(stack.length > 0) {
              var cur = stack.top();
              if (cur.subdirCount > 0) {
                  for (subdir in cur.subdirs) {
                      stack.push(cur.subdirs[subdir]);
                      delete cur.subdirs[subdir];
                  }
                  cur.subdirCount = 0;
              } else {
                  stack.pop();
                  delete cur.files;
                  // other post-order processing
              }
          }
          delete this.subdirs[folder.name];
      }
    }
    

    Though, unless you need to take additional steps when processing deleted files & folders, a simple:

    Folder.prototype.RemoveDir = function (folder) {
      if (this.subdirs[folder.name] === folder) {
        delete this.subdirs[folder.name];
      }
    }
    

    should suffice.

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

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 215k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From my understanding, the guys working on Mono have pretty… May 12, 2026 at 10:59 pm
  • Editorial Team
    Editorial Team added an answer In the online sales application I worked on, we always… May 12, 2026 at 10:59 pm
  • Editorial Team
    Editorial Team added an answer What you have described will not work. One workaround would… May 12, 2026 at 10:59 pm

Related Questions

I was wrestling with some Perl that uses hash references. In the end it
I'm struggling to create a generic (or untyped) array in C (I'm aware that
I have an abstract base class (Comparable) with Date and Time virtually inheriting from
I would like to know what the best Scala imitation of Groovy's safe-dereference operator
I have a container filled with pairs. I want to iterate in it using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.