这里我创建了一个函数,该函数将编译对象的parentid是否与其childId匹配,因此id 1是一个具有11,12的子项的parentid,因此如果我调用该函数checkParentId(obj,1,12),它将返回true,但在这里,我在控制台的其余语句中得到了false的结果,那么,我该如何对该计划进行修改,以便获得预期的结果呢?程序如下。
const obj = [
{
id: 1,
name: "parent 1",
children: [
{
id: 11,
name: "child 1",
children: [
{
id: 12,
name: "grand 1",
children: []
}
]
}
]
},
{
id: 2,
name: "parent 2",
children: [
{
id: 21,
name: "c1",
children: []
}
]
},
];
const checkParentId = (obj, parentId, childId) => {
for (let item of obj) {
if (item.id === parentId) {
return true;
}
if (item.children.length > 0) {
return checkParentId(item.children, parentId, childId);
}
return false;
}
};
console.log(checkParentId(obj, 1, 12)); // true
console.log(checkParentId(obj, 1, 21)); // false
console.log(checkParentId(obj, 1, 11)); // true
console.log(checkParentId(obj, 2, 21)); // true
console.log(checkParentId(obj, 1, 2)); // false
当前,您总是搜索整个obj
您只想在obj
中的特定条目中进行搜索。因此,我认为你需要两个功能。
一个函数将在数组中找到一个人。
另一个函数是实际的父子测试仪:它首先找到父对象,然后在内部查找子对象。
调用数组obj
是在找麻烦
这使得很难跟踪什么是项目列表,什么是项目。
const parents = [ // This is an ARRAY, not an object
{
id: 1,
children: [{
id: 11,
children: [{
id: 12,
children: [],
}, ],
}, ],
},
{
id: 2,
children: [{
id: 21,
children: [],
}, ],
},
];
function findPersonInArray(people, id) { // Make clear that you are expecting an ARRAY of people
let found = null
people.forEach(person => {
if (person.id === id) {
found = person;
}
if (person.children && person.children.length > 0) {
const child = findPersonInArray(person.children, id);
if (child) {
found = child;
}
}
})
return found // This was one line too early in your code, stopping JS from looking in Person 2
};
function checkParentId(parents, parentId, childId) {
const parent = findPersonInArray(parents, parentId);
if (parent) {
if (childId === parentId) { // Special case if parent and child are same
return true;
}
if (findPersonInArray(parent.children, childId)) {
return true;
}
}
return false;
}
console.log(checkParentId(parents, 1, 12)); // true
console.log(checkParentId(parents, 1, 21)); // false
console.log(checkParentId(parents, 1, 11)); // true
console.log(checkParentId(parents, 2, 21)); // true
console.log(checkParentId(parents, 1, 2)); // false
问题是,当您的代码找到父节点时,会返回,但它实际上应该在该节点下方执行递归搜索以找到子节点。
由于您需要搜索两个节点,因此首先定义一个函数会很有用,该函数会在给定要搜索的节点数组的情况下找到 一个这是它的代码:const findNode = (nodes, id) => {
for (const node of nodes) {
const match = node.id === id ? node : findNode(node.children, id);
if (match) return match;
}
};
const checkParentId = (nodes, parentId, childId) => {
const parent = findNode(nodes, parentId);
return !!(parent && findNode(parent.children, childId));
};
// Your example & tests
const obj = [{id: 1,name: "parent 1",children: [{id: 11,name: "child 1",children: [{id: 12,name: "grand 1",children: []}]}]},{id: 2,name: "parent 2",children: [{id: 21,name: "c1",children: []}]},];
console.log(checkParentId(obj, 1, 12)); // true
console.log(checkParentId(obj, 1, 21)); // false
console.log(checkParentId(obj, 1, 11)); // true
console.log(checkParentId(obj, 2, 21)); // true
console.log(checkParentId(obj, 1, 2)); // false