I am trying to make a tree traversal function in javascript. I also want ancestors of any nodes. That's why I have written code like this:
const visit = (tree) => {
if (!typeof ancestors){
const ancestors = []
}
console.log(ancestors)
if (Array.isArray(tree.children)) {
ancestors.push(tree)
tree.children.forEach((e) => {
visit(e);
})
}
}
visit({
"type":"root",
"children":[
{
"type":"element",
"children":[
{
"type":"element"
},
{
"type":"element"
},
{
"type":"element"
},
{
"type":"element"
}
]
},
{
"type":"element"
},
{
"type":"element"
},
{
"type":"element"
}
]
})
But it is giving me error:
Error: ancestors is not defined
Can someone help me what to do? (I don't want to put this ancestors
as property of object.)
const
variables are block-scoped
You can create a wrapper function if you don't want to have a global variable
const visit = (tree) => {
const ancestors = []
const req = (tree) => {
if (Array.isArray(tree.children)) {
ancestors.push(tree)
tree.children.forEach((e) => {
req(e);
})
}
}
req(tree)
return ancestors
}
const result = visit({
"type": "root",
"children": [{
"type": "element",
"children": [{
"type": "element"
},
{
"type": "element"
},
{
"type": "element"
},
{
"type": "element"
}
]
},
{
"type": "element"
},
{
"type": "element"
},
{
"type": "element"
}
]
})
console.log(result)