0
点赞
收藏
分享

微信扫一扫

递归调用方法

Raow1 2022-01-07 阅读 119

使用场景:过滤出数据中不知道会有多少层父子的某个值等,相当于有限循环的获取某个值

// 下面举了一个例子
// 获取arr中所有父子的type
data(){
    return {
    arr:[
        {type:'0',child:{type:'2',child:{type:'3',child:{type:'4',child:{type:'5',...}}}}},
        {type:'0-1',child:{type:'2-1',child:{type:'3-1',child:{type:'4-1',child:{type:'5-1',...}}}}}    
    ],
    typeList:[]   
    }
},
mounted(){
    this.fn(this.arr)
},
methods:{
    // 循环出arr所有的type
   fn(data){
       this.recursiveCall(data)
    },
    recursiveCall(data){
    data.forEach(ele=>{
       this.typeList.push(ele.type)    
       if(ele.child.length>0){
           this.fn(ele.child)       
       }
    })   
    }
}
举报

相关推荐

0 条评论