如何删除/修剪节点中不存在于单独数组中的所有树节点

我有以下 JSON 树表示形式:


let tree = [

    {

        "label": "Org01",

        "children": [

            {

                "label": "Dist01",

                "children": [

                  {

                      "label": "School1",

                      "children": [

                        {

                          "label": "class1",

                          "children": []

                        },

                        {

                          "label": "class2",

                          "children": []

                        }

                      ]

                  },

                  {

                        "label": "School1tst",

                        "children": []

                  }

                ]

            },

            {

                "label": "Dist02",

                "children": []

            }

        ]

    },

    {

        "label": "contoso01",

        "children": [

            {

                "label": "Dist A",

                "children": [

                    {

                        "label": "School A",

                        "children": [

                          {

                            "label": "classA",

                            "children": []

                          }

                        ]

                    },

                    {

                        "label": "School B",

                        "children": [

                          {

                            "label": "classB",

                            "children": []

                          }

                        ]

                    }

                ]

            }

           

我有一个数组中的节点列表,如下所示:


let whitelist = ['class1', 'School1', 'Dist01'];

如何从树中删除上述数组中不存在的所有节点。但是,如果父节点的子节点在白名单中,则需要在树上显示父节点。


从树中删除特定节点对我来说是可能的,但是除了数组中的少数节点之外,我无法找到从树中删除所有节点的方法。


谢谢,我感谢任何帮助。


慕标5832272
浏览 112回答 3
3回答

陪伴而非守候

这应该可以完成工作:function filter(tree, list){    let output = [];    for(i in tree){        if(list.indexOf(tree[i].label) >= 0){            tree[i].children = filter(tree[i].children, list);            output.push(tree[i]);        }else{            output = output.concat(filter(tree[i].children, list));        }    }   return output;}

小唯快跑啊

我这样解决了这个问题:function deleteNodes(tree, list) {    if (tree.length > 0) {            tree.forEach((node, i) => {                this.deleteNodes(node.subItems, list);                if (node.subItems) {                    if (node.subItems.length === 0 && !list.includes(node.text))                     {                        tree.splice(i, 1);                    }                }            });        }}

MM们

const prunedNode = node => {  const pruned = whitelist.includes(node.label) ? node : null;  if (pruned) {    node.children = node.children.reduce((prunedChildren, child) => {      const prunedChildNode = prunedNode(child);      if (prunedChildNode) {        prunedChildren.push(prunedChildNode);      }      return prunedChildren;    }, []);  }  return pruned;};console.log(prunedNode(tree));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript