var res = [{
code: 1,
name: "湖北省",
children: [{
code: 1,
name: "武汉市",
children: [{
code: 1,
name: "汉阳区",
children: [{
code: 1,
name: "水上分局1"
}]
}, {
code: 1,
name: "武昌区",
children: [{
code: 1,
name: "水上分局2"
}]
}, {
code: 1,
name: "汉口区",
children: [{
code: 1,
name: "水上分局3"
}]
}]
}, {
code: 1,
name: "十堰市",
children: [{
code: 1,
name: "郧阳区",
children: [{
code: 1,
name: "安阳镇"
}]
}, {
code: 1,
name: "茅箭区",
children: [{
code: 1,
name: "小川乡"
}]
}]
}]
}]
已知树形结构 res。现在有一个对象是{code: 1,name: "水上分局"},想向上依次获取取得父级,比如说水上分局 父节点是汉阳区 汉阳区父节点是武汉市 武汉市父节点是湖北省 最后将 汉阳区 武汉市 湖北省 存起来 求指点
调用
打印调用结果
随手给你写一个函数吧,这个确实比一般递归复杂那么一点点,就是要记录一下你遍历过的节点。