Here is a data array structure that I am working with:
let data = [{ id: 1, name: '', children: [{ id: 11, name: '', children: [] }, { id: 12, name: '', children: [{ id: 121, name: '', children: [] }, { id: 122, name: '', children: [] }] },
{ id: 2, name: '', children: [{ id: 21, name: '', children: [] }, { id: 22, name: '', children: [{ id: 221, name: '', children: [] }, { id: 222, name: '', children: [] }] }] }];
This structure can extend to multiple levels. In my Vue template, I have implemented a recursive method called validateNodes. This method checks if any object at any level has an empty name field (i.e., name: ''). If an empty name is found, it returns false; otherwise, it returns true.
validateNodes(tree) {
if (tree.length) {
return tree.forEach((elem) => {
if (elem.name === "") return false;
return elem.children.length > 0
? this.validateNodes(elem.children)
: true;
});
} else {
return true;
}
},
validateWbs() {
let data = ....(value specified above);
const valid = this.validateNodes(data);
console.log(valid);
}
However, when I try to print the 'valid' value in the console, it shows as undefined. Can someone please help me fix this issue?
Thank you in advance.