从树

时间:2017-06-01 06:09:56

标签: javascript json angular tree

我正在使用angular-tree-component(https://angular2-tree.readme.io)在Angular2应用程序中显示一个树。

我需要让用户能够选择树中的任何节点并将其删除。这应该删除所选节点及其所有子节点(如果它是叶子节点,那么只是节点)。用户完成后,已修剪的树将被发送到服务器并保存在那里。

在下面的示例中,用户选择节点1-2'并单击“删除”。这将删除节点1-2,节点1'和节点2'节点2'从视图和底层Javascript对象更新。然后,用户可以单击另一个按钮(例如,保存)以将更新的对象保存在服务器上。

Node selected in Tree

为了实现这一点,我编写了以下代码,单击“删除”按钮即可调用该代码。我存储所选节点的id,然后遍历整个树以找到该id。当id匹配时,我使用'删除'要删除的关键字' id'和'文字'。但是,删除儿童'不起作用。

deleteSelectedNode() { 
    //Get the node which was selected by the user  
    let nodeToDelete: TreeNode = this.myService.getActiveNode();
    //id will be unique, can be used to identify the node
    let idToDelete = nodeToDelete.data.id; 
    this.traverseTree(this.nodes[0], idToDelete);
  }

  traverseTree(obj, idToDelete: string) {

    for (var k in obj) {
      if (typeof obj[k] == "object" && obj[k] !== null) {
        this.traverseTree(obj[k], idToDelete);
      }

      else {
        if (k === 'id') {
          let id = obj[k];
          if (id === idToDelete) {
            console.log('ID Matched ' + id);
            delete obj['id'];
            delete obj['text'];
            delete obj['children']; 
            break;
          }
        } 
      }
    }
  }

下面是渲染树的JSON。

{
        "children": [
            {
                "children": [
                    {
                        "text": "Node 1",
                        "id": "A493"
                    },
                    {
                        "text": "Node 2",
                        "id": "A494"
                    }
                ],
                "text": "Node 1-2",
                "id": "A491"
            },
            {
                "children": [
                    {
                        "children": [
                            {
                                "text": "Node 3",
                                "id": "A5ab"
                            },
                            {
                                "text": "Node 4",
                                "id": "A5ac"
                            },
                            {
                                "text": "Node 5",
                                "id": "A5ad"
                            },
                            {
                                "text": "Node 6",
                                "id": "A5ae"
                            },
                            {
                                "text": "Node 7",
                                "id": "A5af"
                            },
                            {
                                "text": "Node 8",
                                "id": "A5b0"
                            },
                            {
                                "text": "Node 9",
                                "id": "A5b1"
                            },
                            {
                                "text": "Node 10",
                                "id": "A5b2"
                            },
                            {
                                "text": "Node 11",
                                "id": "A5b3"
                            },
                            {
                                "text": "Node 12",
                                "id": "A5b4"
                            },
                            {
                                "text": "Node 13",
                                "id": "A6da"
                            },
                            {
                                "text": "Node 14",
                                "id": "A6db"
                            }
                        ],
                        "text": "Node 1-14",
                        "id": "A495"
                    },
                    {
                        "children": [
                            {
                                "text": "Node 15",
                                "id": "A6dc"
                            },
                            {
                                "text": "Node 16",
                                "id": "A6dd"
                            },
                            {
                                "text": "Node 17",
                                "id": "A6de"
                            },
                            {
                                "text": "Node 18",
                                "id": "A6df"
                            },
                            {
                                "text": "Node 19",
                                "id": "A6e0"
                            },
                            {
                                "text": "Node 20",
                                "id": "A6e1"
                            },
                            {
                                "text": "Node 21",
                                "id": "A6e2"
                            },
                            {
                                "text": "Node 22",
                                "id": "A6e3"
                            }
                        ],
                        "text": "Node 15-22",
                        "id": "A496"
                    },
                    {
                        "text": "Node 23",
                        "id": "A497"
                    },
                    {
                        "text": "Node 24",
                        "id": "A498"
                    }
                ],
                "text": "Node 23-24",
                "id": "A492"
            }
        ],
        "text": "Node 1-24",
        "id": "A490"
    }

如何删除整个节点,包括儿童?另外,有没有更好的方法来实现这一目标?

1 个答案:

答案 0 :(得分:0)

您可以通过父参考

轻松完成
const deepMap = (arr, fn) => {
  return arr.reduce((first, second) => first.concat(Array.isArray(second) ? [deepMap(second, fn)] : fn(second)), []);  
}

const res = deepMap([4,5, [3,4,[2]]], x => x + 5);

console.log(res)