为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕标5832272 2022-09-23 09:34:47
我有以下 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'];如何从树中删除上述数组中不存在的所有节点。但是,如果父节点的子节点在白名单中,则需要在树上显示父节点。从树中删除特定节点对我来说是可能的,但是除了数组中的少数节点之外,我无法找到从树中删除所有节点的方法。谢谢,我感谢任何帮助。
查看完整描述

3 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

这应该可以完成工作:


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;

}


查看完整回答
反对 回复 2022-09-23
?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

我这样解决了这个问题:


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

                    }

                }

            });

        }

}


查看完整回答
反对 回复 2022-09-23
?
MM们

TA贡献1886条经验 获得超2个赞

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


查看完整回答
反对 回复 2022-09-23
  • 3 回答
  • 0 关注
  • 111 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号