targetd( )
根据ID找到数据中的url
childrenFilter( )
使用递归改变原数组结构
<template>
<div @click="recursion(id,list)"></div>
</template>
data(){
return{
id:3.1,
list:[
{id:1,url:"a",children:[id:2,url:"a",children:[]]},
{id:2,url:"b",},
{id:3,url:"c",children:[id:3.1,url:"c.c",children:[]]},
],
targetUrl:"";
}
},
methods:{
targetd(opid, list) {
if (list && list.length > 0) {
for (let i = 0; i < list.length; i++) {
if (list[i].id === opid) {
this.targetUrl = list[i].url;
return;
} else {
if (list[i].children) {
this.targetd(opid, list[i].children);
}
}
}
}
},
childrenFilter(list) {
if (list && list.length > 0) {
for (let k in list) {
if (list[k].children != null && list[k].children != []) {
list[k].children = this.childrenFilter(list[k].children);
}
}
list = list.filter(o => {
if(o.id=="2")o.url="url"
return true
});
return list;
}
},
}