js遞歸過濾樹的常用方法(根據(jù)條件去掉樹的某些節(jié)點)

1.改變鍵值對

filterTree(treeArr) {
      function eachItem(arr) {
        let res = arr.filter(item => {
          if (item.children && item.children.length > 0) {
            item.children = eachItem(item.children)
          }
          item.key = item.id
          return item
        })
        return res
      }
      return eachItem(treeArr)
    },
    //[
    //     {
    //         key: 6,
    //         path: "/modal",
    //         name: "彈窗",
    //         component: "modal/index",
    //         title: "彈窗", icon: "home", children: []
    //       }
    //]
    
    //[
    //     {
    //         id: 6,
    //         path: "/modal",
    //         name: "彈窗",
    //         component: "modal/index",
    //         title: "彈窗", icon: "home", children: []
    //       }
    //]
    

2.根據(jù)條件保留某些節(jié)點

從樹tree中過濾掉,樹的key不在bList數(shù)組中的節(jié)點

        filterTreeArray(tree, bList) {
            return tree.filter(item => {
                return bList.indexOf(item.key) > -1
            }).map(item => {
                item = Object.assign({}, item)
                if (item.children) {
                    item.children = this.filterTreeArray(item.children, bList)
                }
                return item
            })
        }
      // let menuData = this.filterTreeArray(this.treeData, menuCheckedKyes)   
/*
this.treeData=[
    {
        key: 2,
        name: "構(gòu)建類管理",
    },
    {
        name: "三級菜單",
        key: 3,
        children: [
            {
                key: 34,
                name: "routeview",
                children: [
                    {
                        key: 35,
                        name: "菜單管理",
                        children: []
                    }
                ]
            },
             {
                key: 36,
                name: "組織管理",
                children: []
            },
        ]
    },
]
menuCheckedKyes=[2,3 , 34]  只要key是2、3、34的
過濾后
this.treeData=[
    {
        key: 2,
        name: "構(gòu)建類管理",
    },
    {
        name: "三級菜單",
        key: 3,
        children: [
            {
                key: 34,
                name: "routeview",
                children: []
            },
        ]
    },
]

*/


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Java繼承關(guān)系初始化順序 父類的靜態(tài)變量-->父類的靜態(tài)代碼塊-->子類的靜態(tài)變量-->子類的靜態(tài)代碼快-->父...
    第六象限閱讀 2,246評論 0 9
  • Remove time complexity: remove from a set is O(1), remove...
    云端漫步_b5aa閱讀 726評論 0 0
  • 一、CSS問題 1.flex布局 display:flex; 在父元素設(shè)置,子元素受彈性盒影響,默認(rèn)排成一行,如果...
    陳二狗想吃肉閱讀 789評論 0 9
  • MySQL-InnoDB 架構(gòu) CheckPoint 已經(jīng)被flush到頁上的LSN。 刷盤策略 縮短數(shù)據(jù)庫恢復(fù)時...
    Yves_Chen閱讀 321評論 0 0
  • php usleep() 函數(shù)延遲代碼執(zhí)行若干微秒。 unpack() 函數(shù)從二進(jìn)制字符串對數(shù)據(jù)進(jìn)行解包。 uni...
    思夢PHP閱讀 2,133評論 1 24

友情鏈接更多精彩內(nèi)容