Javascript - 从外部数组中删除对我的对象的引用

发布于 2024-08-11 08:32:09 字数 1027 浏览 2 评论 0原文

我在取消引用 Javascript 对象并将其设置为 NULL 时遇到问题。

在这里,我有一个支持递归子目录删除的文件夹实现。请参阅我的评论以了解我的困境。

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?
      }
   }
}

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?
      }
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奢欲 2024-08-18 08:32:10

javascript中的一切都是按值传递的,所以“*cur=NULL”是不可能的。您基本上有以下选项

  • :按照您的建议使用parentID,
  • 如果您的文件夹层次结构有一个众所周知的根,从该根浏览以查找父对象,
  • 使用诸如DOMremoveChild(在父级上调用)之类的东西,而不是removeNode(在节点本身上调用)。

Everything is javascript is passed by value, so "*cur=NULL" is not possible. You basically have the following options here

  • use parentID as you suggested
  • if your Folder hierarchy has a well-known root, browse from that root to find the parent object
  • use something like DOM removeChild (which is called on parent), instead of removeNode (which is called on the node itself).
泡沫很甜 2024-08-18 08:32:10

今天我也想做同样的事情。
我通过将对象的索引存储为对象本身的属性来解决这个问题。

当你添加它时:

myObj.ID = myArr.push(myObj);

所以要删除它,

myArr[myObj.ID] = null;

我想你现在已经解决了它,但你几乎可以做同样的事情;而且它比使用对象更简单。

I was trying to do the same thing today.
I've worked around it by storing the object's index as a property of the object itself.

When you add it:

myObj.ID = myArr.push(myObj);

So to remove it you

myArr[myObj.ID] = null;

I guess you solved it by now, but you could do almost the same; and it's simpler than using objects.

我早已燃尽 2024-08-18 08:32:09

请注意,您的问题并不像您怀疑的那么大,因为 RemoveDir 中除 folder 之外的所有子目录都将从其父目录的 subdir 中删除> 通过 stack.push(cur.subdirs.pop());

要在父目录中查找子目录,您可以使用对象作为字典而不是数组 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);
}

给定一个文件夹,您可以使用以下方法从父文件夹中删除该文件夹:

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

这是预购版本:

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;
  }
}

递归后购版本:

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;
    }
}

迭代后购版本:

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];
  }
}

不过,除非您需要采取其他步骤处理已删除的文件时&文件夹,一个简单的:

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

应该足够了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文