Trying to filter out nodes in a recursion function that iterates through a tree based on the registry property.
function reduceNodesRegistry(source: any) {
if (!source.registry) return source;
return {
...source,
children:
source.children &&
source.children
.map(reduceNodesRegistry)
.filter((item: any) => item.registry),
};
}
console.clear();
console.log("##########");
console.log(reduceNodesRegistry(source));
Full code with model by link
Wondering why nodes with registry: false
are not being hidden?
Edition 1:
reduceNodesRegistry(source: any) {
if (typeof source.registry !== "undefined" && source.registry === false)
return source;
return {
...source,
children:
source.children &&
source.children
.map(this.reduceNodesRegistry.bind(this))
.filter(
(item: any) =>
typeof item.registry === undefined || item.registry === true
),
};
}
Still not hiding nodes where registry: false
.
Third attempt:
reduceNodesRegistry(node: any) {
return {
...node,
children:
node.children &&
node.children
.map(this.reduceNodesRegistry.bind(this))
.filter(
(node: any) => node.registry || node.registry === "undefined"
),
};
}