У меня есть объект Node, у которого есть метод remove, который рекурсивно удаляет узлы. Но когда он завершает работу и возвращает истину, он снова возвращается к циклу и тоже удаляет родителя. Что не так с методом?
// Node object
class Node {
id;
parentId;
children: Node[];
// Clear method
public clear(node: any, action: any) {
if (Library.isDefined(node)) {
for (let nI = node.children.length - 1; nI > -1; nI--) {
this.clear(node.children[nI], action);
action(node);
node.children.pop();
}
}
}
// Remove method
public remove(obj, action, condition) {
return this._remove(this, obj, action, condition);
}
private _remove(root, obj, action, condition) {
let removed = false;
if (root !== null && !removed) {
if (condition(root, obj)) {
this.clear(obj, action);
removed = true;
} else {
for (let nI = root.children.length - 1; nI > -1 && !removed; nI--) {
removed = this._remove(root.children[nI], obj, action, condition);
if (removed) {
root.children.splice(nI, 1);
}
}
}
}
return removed;
}
}