简体   繁体   English

如何通过属性修改树 object?

[英]How to modify tree object by property?

I have an object with type:我有一个 object 类型:

export interface TreeNode extends ITreeNode {
   name: string,
   children:  TreeNode[],
   show?: boolean;
}

I need to reduce this object by property show and return a new tree where show is true or undefined .我需要通过属性show减少这个 object 并返回一个新树,其中showtrueundefined

I have tried this:我试过这个:

  function prepareNodes(source: TreeNode) {
        if (source.show!== undefined || source.show == false) delete source;
      if (source.children) {
        source.children.forEach((child: TreeNode) => {
          this.prepareNodes(child);
        });
      }
  }

Also I tried:我也试过:

function prepareNodes(source: any) {
      if (source.show !== undefined && source.show === false) source = null;
      if (source.children) {
        source.children = source.children.filter((child: any) => child.show== undefined || child.show === true);
        source.children.forEach((child: any) => prepareNodes(child));
      }
  }

Currently my assumption is that you want to produce a new tree containing just those nodes of the original tree where the show property is either true or undefined for that node and all ancestor nodes .目前我的假设是您想要生成一个新树,其中仅包含原始树的那些节点,其中该节点和所有祖先节点show属性为trueundefined So if show is false on any node, the output tree will not contain that node or any subtree of that node.因此,如果任何节点上的show为 false,则 output 树将不包含该节点或该节点的任何子树。

I also assume that it's possible for the root node to have show of false , in which case the whole tree might end up undefined .我还假设根节点可能showfalse ,在这种情况下,整个树可能会以undefined告终。 You can't set modify an object to become undefined ;您不能设置修改 object 为undefined you can change its contents, but you can't delete it.您可以更改其内容,但不能删除它。 So I'm not going to present anything that tries to modify the original tree.所以我不会介绍任何试图修改原始树的东西。 I won't touch the original tree.我不会碰原来的树。 Instead I will produce a completely new tree.相反,我将生成一棵全新的树。

Here goes:开始:

const defined = <T,>(x: T | undefined): x is T => typeof x !== "undefined";

function filterTree(source: TreeNode): TreeNode | undefined {
  if (source.show === false) return;
  return {
    name: source.name,
    show: source.show,
    children: source.children.map(filterTree).filter(defined)
  }
}

The filterTree() function will return undefined if the argument node's show property is exactly false (and not undefined ).如果参数节点的show属性完全是false (而不是undefined ), filterTree() function 将返回undefined Otherwise, it produces a new node with the same name and show , and whose children property is what you get when you call filterTree() (recursively) on each of the original node's children , and then filter out any undefined nodes.否则,它会生成一个具有相同nameshow的新节点,其children属性是您在每个原始节点的children上调用filterTree() (递归)时得到的,然后filter掉任何undefined的节点。

I'm using a user-defined type guard function called defined to let the compiler know that the filter ing takes an array of TreeNode | undefined我正在使用一个名为defined用户定义类型保护function 让编译器知道filter需要一个TreeNode | undefined数组。 TreeNode | undefined and produces an array of TreeNode , eliminating any undefined entries. TreeNode | undefined并生成一个TreeNode数组,消除任何undefined的条目。

Hopefully this meets your use cases;希望这符合您的用例; please test it out on whatever data you have and check, because the question unfortunately did not include such data.请根据您拥有的任何数据进行测试并检查,因为不幸的是问题不包括此类数据。

Playground link to code Playground 代码链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM