let list = [{"name":"福建省","children":[{"name":"厦门市","children":[{"name":"思明区"},{"name":"同安区"},{"name":"海沧区"},{"name":"湖里区"}]},{"name":"福州市","children":[{"name":"苍山区"},{"name":"鼓楼区"},{"name":"马尾区"}]}]},{"name":"广东省","children":[{"name":"深圳市","children":[{"name":"南山区"},{"name":"宝安区"}]}]}]
function getObjByTree (data, id) {
let result = null
if (!data) return // return; 中断执行
for (let i in data) {
if (result !== null) break
let item = data[i];
if (item.name=== id) {
result = item;
break;
} else if (item.children&&item.children.length) {
result = getObjByTree(item.children, id);
}
}
return result;
}
console.log(getObjByTree(list,'思明区'))
console