let currentVal = '1-1-3-2'
let list = [{
text: "1省",
value: "1",
child: [{
text: "1市",
value: "1-1",
child: [{
text: "1区",
value: "1-1-1",
child: [{
text: "1区-1镇",
value: "1-1-1-1",
}, ],
},
{
text: "2区",
value: "1-1-2",
child: [{
text: "2区-1镇",
value: "1-1-2-1",
}, ],
},
{
text: "3县",
value: "1-1-3",
child: [{
text: "3县-1镇",
value: "1-1-3-1",
}, {
text: "3县-2镇",
value: "1-1-3-2",
}],
},
],
}, ],
}, ];
function recursive(list, value) {
let obj = {};
for (let i = 0; i < list.length; i++) {
// console.log('list[i]', list[i])
if (list[i].value === value) {
// console.log('list[i].value === value', list[i])
return obj = list[i];
}
else if (list[i].child && list[i].child.length >= 1) {
obj = recursive(list[i].child, value);
// console.log('obj.value', obj, list)
if (obj.value) {
return obj;
}
}
}
console.log('retrunObj', list, obj)
return obj;
}
console.log('结果', recursive(list, currentVal))
tree 数据结构 已知当前值 递归找到当前值所在的对象
最新推荐文章于 2024-04-18 16:10:58 发布