使用场景:过滤出数据中不知道会有多少层父子的某个值等,相当于有限循环的获取某个值
// 下面举了一个例子
// 获取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)
}
})
}
}