I have an object with type:
export interface TreeNode extends ITreeNode {
name: string,
children: TreeNode[],
show?: boolean;
}
I need to reduce this object by property show
and return a new tree where show
is true
or undefined
.
I have tried this:
function prepareNodes(source: TreeNode) {
if (source.show!== undefined || source.show == false) delete source;
if (source.children) {
source.children.forEach((child: TreeNode) => {
this.prepareNodes(child);
});
}
}
Also I tried:
function prepareNodes(source: any) {
if (source.show !== undefined && source.show === false) source = null;
if (source.children) {
source.children = source.children.filter((child: any) => child.show== undefined || child.show === true);
source.children.forEach((child: any) => prepareNodes(child));
}
}