如何在javascript中检索家谱树中具有嵌套子节点的每个节点?
为了完成这棵树,我创建了Add_fixer、Add_human和Add_child等方法,在从上述节点开始添加和完成图像中给定的树时,我正在调用这些方法。然后,我检索给定输入名称的关系(儿子、女儿、兄弟姐妹、姻亲等) 但是在一个级别添加妻子对象时,我遇到了一个错误:如何在javascript中检索家谱树中具有嵌套子节点的每个节点?,javascript,recursion,Javascript,Recursion,为了完成这棵树,我创建了Add_fixer、Add_human和Add_child等方法,在从上述节点开始添加和完成图像中给定的树时,我正在调用这些方法。然后,我检索给定输入名称的关系(儿子、女儿、兄弟姐妹、姻亲等) 但是在一个级别添加妻子对象时,我遇到了一个错误:TypeError:cannotreadproperty'children'undefined async function addWifeOperation(str){ //assigning and sending parame
TypeError:cannotreadproperty'children'undefined
async function addWifeOperation(str){ //assigning and sending parameters to wifebject before addWife
var husbandName = str[1]; //'Chit'
var wifeName = str[2]; //'Amba'
const display = async(person) => {
if(person.name == husbandName) {
var wife = {
name:wifeName,
husband:husbandName,
fil:person.father,
mil:person.mother,
children : []
}
person.wife = wife;
}
if (person.wife.children && person.wife.children.length && typeof person.wife.children === "object") {
console.log("person.wife = "+ JSON.stringify(person.wife));
person.wife.children.forEach(display);
}
}
}
person.layer=妻子代码>可能无法执行,因此person.fixer
可能无法定义。
最好检查person.layer
是否未定义
if(个人、妻子){
if(person.fixer.children&&person.fixer.children.length&&typeof person.fixer.children==“对象”){
log(“person.wife=“+JSON.stringify(person.wife));
个人。妻子。孩子。家庭(展示);
}
}
或者任何其他方式,我可以使用javascript动态创建此树,然后在定义input.person.fixer时检索关系中的名称基础。我没有收到错误消息。您的错误消息说,person.fixer
是未定义的。如果(person.aird.children&&person.aird.children.length&&typeof person.aird.children==“object”){
,您的错误是否发生在第行中?我收到此错误:TypeError:无法读取未定义的属性“children”:此行中出现错误:person.aird.children.forEach(display);
async function addWifeOperation(str){ //assigning and sending parameters to wifebject before addWife
var husbandName = str[1]; //'Chit'
var wifeName = str[2]; //'Amba'
const display = async(person) => {
if(person.name == husbandName) {
var wife = {
name:wifeName,
husband:husbandName,
fil:person.father,
mil:person.mother,
children : []
}
person.wife = wife;
}
if (person.wife.children && person.wife.children.length && typeof person.wife.children === "object") {
console.log("person.wife = "+ JSON.stringify(person.wife));
person.wife.children.forEach(display);
}
}
}