【发布时间】:2021-05-15 10:16:48
【问题描述】:
我有一个对象类型:
export interface TreeNode extends ITreeNode {
name: string,
children: TreeNode[],
show?: boolean;
}
我需要通过属性show 减少这个对象并返回一个新树,其中show 是true 或undefined。
我试过这个:
function prepareNodes(source: TreeNode) {
if (source.show!== undefined || source.show == false) delete source;
if (source.children) {
source.children.forEach((child: TreeNode) => {
this.prepareNodes(child);
});
}
}
我也试过了:
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));
}
}
【问题讨论】:
-
更新了我的问题
-
你期望输出是什么?该函数目前给出的输出是什么?
-
show不能是您自己定义的undefined。 -
你能发布一些示例数据吗?
标签: javascript typescript