亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何刪除/修剪節點中不存在于單獨數組中的所有樹節點

如何刪除/修剪節點中不存在于單獨數組中的所有樹節點

慕標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 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號