在 JSON 数组中递归查找 parents 按前一个值向后连接
recursively find parents in JSON array backward connect by prev value
[
{
"secCode": 2,
"secName": "GENERAL NURSING CARE SECTION",
"prevSec": 0,
},
{
"secCode": 1,
"secName": "CRITICAL CARE NURSING SECTION",
"prevSec": 0,
"children": [
{
"secCode": 3,
"secName": "OPERATION THEATRE",
"prevSec": 1,
"children": [
{
"secCode": 5,
"secName": "MAIN OPERATION THEATRE",
"prevSec": 3,
"estCode": 152,
},
{
"secCode": 6,
"secName": "DAY CARE DT SERVICE",
"prevSec": 3,
}
]
},
{
"secCode": 4,
"secName": "CRITICAL CARE SERVICES",
"prevSec": 1,
"children": [
{
"secCode": 675,
"secName": "Test",
"prevSec": 4,
"children": [
{
"secCode": 676,
"secName": "Test1",
"prevSec": 675,
},
{
"secCode": 677,
"secName": "Test 2",
"prevSec": 675,
},
{
"secCode": 678,
"secName": "Test 3",
"prevSec": 675,
},
{
"secCode": 679,
"secName": "Test 4",
"prevSec": 675,
}
]
},
{
"secCode": 7,
"secName": "ACUTE CARE",
"prevSec": 4,
}
]
}
]
}
]
这是树json树结构。这里每个节点使用 prevSec 值向后递归地连接到它的 parent。
对于 secCode 7,它的 parents 树将是 [7, 4, 1] - 通过 secCode = prevSec 连接。
对于 secCode 679,它的 parents 将是 [679, 675, 4, 1]
对于 secCode 2,其 parents 将是 [2]
我试过这个解决方案,它是部分的
getParent(arr, childSecCode) {
if (childSecCode == 0) {
return;
} else {
let val = arr.find(item => {
childSecCode === item.secCode;
});
if (val) {
//arr.find(item => childSecCode == item.secCode).showChildren = true;
/* if(check ==0)
arr.find(item => childSecCode == item.secCode).className = "selected"; */
this.getParent(arr, val.prevSec);
}
}
}
这将不起作用,因为 find 不检查 children。
怎样才能达到想要的效果。
你需要像这样递归遍历你的数据结构。
getParent(arr, childSecCode){
for(let i=0; i<arr.length;i++) {
const item = arr[i];
if(item.secCode === childSecCode)
return item.prevSec; // found item
if(item.children) {
const prevSec = getParent(item.children, childSecCode);
if(prevSec) return prevSec;
}
}
}
此方法使用深度优先算法。
如果你想避免在需要时搜索整棵树,最好先预处理你的树,这样你就可以在常数时间内直接找到任何节点 (object),鉴于其 secCode
.
您可以为 secCode
和节点之间的连接创建一个 Map
:
// Creates a map which is keyed by secCode, and for a secCode provides
// the corresponding node from the tree
function createMap(tree) {
let map = new Map;
function recur(node) {
map.set(node.secCode, node);
if (node.children) node.children.forEach(recur);
}
tree.forEach(recur);
return map;
}
// Uses the map to walk up the tree
function getParents(secCode, map) {
let parents = [];
while (secCode) {
parents.push(secCode);
let node = map.get(secCode);
secCode = node.prevSec;
}
return parents;
}
// The tree from the question:
let tree = [{"secCode": 2,"secName": "GENERAL NURSING CARE SECTION","prevSec": 0,},{"secCode": 1,"secName": "CRITICAL CARE NURSING SECTION","prevSec": 0,"children": [{"secCode": 3,"secName": "OPERATION THEATRE","prevSec": 1,"children": [{"secCode": 5,"secName": "MAIN OPERATION THEATRE","prevSec": 3,"estCode": 152,},{"secCode": 6,"secName": "DAY CARE DT SERVICE","prevSec": 3,}]},{"secCode": 4,"secName": "CRITICAL CARE SERVICES","prevSec": 1,"children": [{"secCode": 675,"secName": "Test","prevSec": 4,"children": [{"secCode": 676,"secName": "Test1","prevSec": 675,},{"secCode": 677,"secName": "Test 2","prevSec": 675,},{"secCode": 678,"secName": "Test 3","prevSec": 675,},{"secCode": 679,"secName": "Test 4","prevSec": 675,}]},{"secCode": 7,"secName": "ACUTE CARE","prevSec": 4,}]}]}];
// Preprocessing
let map = createMap(tree);
// Example calls:
console.log(getParents(7, map));
console.log(getParents(679, map));
请注意,您示例中的树没有 4 作为 3 的 child,正如您的示例所暗示的那样 - 4 是 1 的 child。
如果你有 class,你当然可以在其构造函数中创建该映射并将其分配给 属性:
class Tree {
constructor(data) {
this.map = new Map;
const recur = (node) => {
this.map.set(node.secCode, node);
if (node.children) node.children.forEach(recur);
}
data.forEach(recur);
}
getParents(secCode) {
let parents = [];
while (secCode) {
parents.push(secCode);
secCode = this.map.get(secCode).prevSec;
}
return parents;
}
}
// The tree from the question:
let data = [{"secCode": 2,"secName": "GENERAL NURSING CARE SECTION","prevSec": 0,},{"secCode": 1,"secName": "CRITICAL CARE NURSING SECTION","prevSec": 0,"children": [{"secCode": 3,"secName": "OPERATION THEATRE","prevSec": 1,"children": [{"secCode": 5,"secName": "MAIN OPERATION THEATRE","prevSec": 3,"estCode": 152,},{"secCode": 6,"secName": "DAY CARE DT SERVICE","prevSec": 3,}]},{"secCode": 4,"secName": "CRITICAL CARE SERVICES","prevSec": 1,"children": [{"secCode": 675,"secName": "Test","prevSec": 4,"children": [{"secCode": 676,"secName": "Test1","prevSec": 675,},{"secCode": 677,"secName": "Test 2","prevSec": 675,},{"secCode": 678,"secName": "Test 3","prevSec": 675,},{"secCode": 679,"secName": "Test 4","prevSec": 675,}]},{"secCode": 7,"secName": "ACUTE CARE","prevSec": 4,}]}]}];
// Preprocessing
let tree = new Tree(data);
// Example calls:
console.log(tree.getParents(7));
console.log(tree.getParents(679));
[
{
"secCode": 2,
"secName": "GENERAL NURSING CARE SECTION",
"prevSec": 0,
},
{
"secCode": 1,
"secName": "CRITICAL CARE NURSING SECTION",
"prevSec": 0,
"children": [
{
"secCode": 3,
"secName": "OPERATION THEATRE",
"prevSec": 1,
"children": [
{
"secCode": 5,
"secName": "MAIN OPERATION THEATRE",
"prevSec": 3,
"estCode": 152,
},
{
"secCode": 6,
"secName": "DAY CARE DT SERVICE",
"prevSec": 3,
}
]
},
{
"secCode": 4,
"secName": "CRITICAL CARE SERVICES",
"prevSec": 1,
"children": [
{
"secCode": 675,
"secName": "Test",
"prevSec": 4,
"children": [
{
"secCode": 676,
"secName": "Test1",
"prevSec": 675,
},
{
"secCode": 677,
"secName": "Test 2",
"prevSec": 675,
},
{
"secCode": 678,
"secName": "Test 3",
"prevSec": 675,
},
{
"secCode": 679,
"secName": "Test 4",
"prevSec": 675,
}
]
},
{
"secCode": 7,
"secName": "ACUTE CARE",
"prevSec": 4,
}
]
}
]
}
]
这是树json树结构。这里每个节点使用 prevSec 值向后递归地连接到它的 parent。 对于 secCode 7,它的 parents 树将是 [7, 4, 1] - 通过 secCode = prevSec 连接。 对于 secCode 679,它的 parents 将是 [679, 675, 4, 1] 对于 secCode 2,其 parents 将是 [2]
我试过这个解决方案,它是部分的
getParent(arr, childSecCode) {
if (childSecCode == 0) {
return;
} else {
let val = arr.find(item => {
childSecCode === item.secCode;
});
if (val) {
//arr.find(item => childSecCode == item.secCode).showChildren = true;
/* if(check ==0)
arr.find(item => childSecCode == item.secCode).className = "selected"; */
this.getParent(arr, val.prevSec);
}
}
}
这将不起作用,因为 find 不检查 children。
怎样才能达到想要的效果。
你需要像这样递归遍历你的数据结构。
getParent(arr, childSecCode){
for(let i=0; i<arr.length;i++) {
const item = arr[i];
if(item.secCode === childSecCode)
return item.prevSec; // found item
if(item.children) {
const prevSec = getParent(item.children, childSecCode);
if(prevSec) return prevSec;
}
}
}
此方法使用深度优先算法。
如果你想避免在需要时搜索整棵树,最好先预处理你的树,这样你就可以在常数时间内直接找到任何节点 (object),鉴于其 secCode
.
您可以为 secCode
和节点之间的连接创建一个 Map
:
// Creates a map which is keyed by secCode, and for a secCode provides
// the corresponding node from the tree
function createMap(tree) {
let map = new Map;
function recur(node) {
map.set(node.secCode, node);
if (node.children) node.children.forEach(recur);
}
tree.forEach(recur);
return map;
}
// Uses the map to walk up the tree
function getParents(secCode, map) {
let parents = [];
while (secCode) {
parents.push(secCode);
let node = map.get(secCode);
secCode = node.prevSec;
}
return parents;
}
// The tree from the question:
let tree = [{"secCode": 2,"secName": "GENERAL NURSING CARE SECTION","prevSec": 0,},{"secCode": 1,"secName": "CRITICAL CARE NURSING SECTION","prevSec": 0,"children": [{"secCode": 3,"secName": "OPERATION THEATRE","prevSec": 1,"children": [{"secCode": 5,"secName": "MAIN OPERATION THEATRE","prevSec": 3,"estCode": 152,},{"secCode": 6,"secName": "DAY CARE DT SERVICE","prevSec": 3,}]},{"secCode": 4,"secName": "CRITICAL CARE SERVICES","prevSec": 1,"children": [{"secCode": 675,"secName": "Test","prevSec": 4,"children": [{"secCode": 676,"secName": "Test1","prevSec": 675,},{"secCode": 677,"secName": "Test 2","prevSec": 675,},{"secCode": 678,"secName": "Test 3","prevSec": 675,},{"secCode": 679,"secName": "Test 4","prevSec": 675,}]},{"secCode": 7,"secName": "ACUTE CARE","prevSec": 4,}]}]}];
// Preprocessing
let map = createMap(tree);
// Example calls:
console.log(getParents(7, map));
console.log(getParents(679, map));
请注意,您示例中的树没有 4 作为 3 的 child,正如您的示例所暗示的那样 - 4 是 1 的 child。
如果你有 class,你当然可以在其构造函数中创建该映射并将其分配给 属性:
class Tree {
constructor(data) {
this.map = new Map;
const recur = (node) => {
this.map.set(node.secCode, node);
if (node.children) node.children.forEach(recur);
}
data.forEach(recur);
}
getParents(secCode) {
let parents = [];
while (secCode) {
parents.push(secCode);
secCode = this.map.get(secCode).prevSec;
}
return parents;
}
}
// The tree from the question:
let data = [{"secCode": 2,"secName": "GENERAL NURSING CARE SECTION","prevSec": 0,},{"secCode": 1,"secName": "CRITICAL CARE NURSING SECTION","prevSec": 0,"children": [{"secCode": 3,"secName": "OPERATION THEATRE","prevSec": 1,"children": [{"secCode": 5,"secName": "MAIN OPERATION THEATRE","prevSec": 3,"estCode": 152,},{"secCode": 6,"secName": "DAY CARE DT SERVICE","prevSec": 3,}]},{"secCode": 4,"secName": "CRITICAL CARE SERVICES","prevSec": 1,"children": [{"secCode": 675,"secName": "Test","prevSec": 4,"children": [{"secCode": 676,"secName": "Test1","prevSec": 675,},{"secCode": 677,"secName": "Test 2","prevSec": 675,},{"secCode": 678,"secName": "Test 3","prevSec": 675,},{"secCode": 679,"secName": "Test 4","prevSec": 675,}]},{"secCode": 7,"secName": "ACUTE CARE","prevSec": 4,}]}]}];
// Preprocessing
let tree = new Tree(data);
// Example calls:
console.log(tree.getParents(7));
console.log(tree.getParents(679));