vue递归过滤树结构数组

let arr=[{
                    title:'1',
                    key:'1',
                    type:0,
                    children:[{
                        title:'1-1',
                        key:'1-1',
                        type:0,
                    }]
                },{
                    title:'2',
                    key:'2',
                    type:0,
                    children:[]
                },{
                    title:'3',
                    key:'3',
                    type:0,
                    children:[{
                        title:'3-1',
                        key:'3',
                        type:0,
                    }]
                },{
                    title:'4',
                    key:'4',
                    type:1,
                    children:[]
                }]
// 过滤出数组type==0的数据
let sul=that.recursiveFilter(arr,0)
/**
             * 递归过滤嵌套数组(树形结构)
             * @param arr {Array}要过滤的原始数据
             * @param type {Boolean} 你要过滤的关键词
             * 
             */
            recursiveFilter(arr,type){
                let data=arr.filter(item=> item.type==type).map((item)=>{
                    item=Object.assign({},item)
                    if(item.children){
                        //递归循环
                        item.children=that.recursiveFilter(item.children,type)
                    }
                    return item
                })
                return data
            },

 

posted @ 2020-12-17 13:54  奔跑吧前端(李钊)  阅读(2040)  评论(0编辑  收藏  举报