问题描述
在下面的代码中,我想做类似的事情
if (key === 'Almond Meal flour'){
continue
}
因此不会为此键创建任何对象/节点。我该怎么办?
export class TodoItemNode {
children: TodoItemNode[];
item: string;
}
export class TodoItemFlatNode {
item: string;
level: number;
expandable: boolean;
}
const TREE_DATA = {
A: {
'Almond Meal flour': null,'Organic eggs': null,'Protein Powder': null,Fruits: {
Apple: null,Berries: ['BlueBerry','RaspBerry'],Orange: null
}
},B: {
'Almond Meal flour': null,Orange: null
}
}
};
buildfiletree(obj: {[key: string]: any},level: number): TodoItemNode[] {
return Object.keys(obj).reduce<TodoItemNode[]>((accumulator,key) => {
// if (key === 'Almond Meal flour'){
// continue
// }
const value = obj[key];
const node = new TodoItemNode();
node.item = key;
if (value != null) {
if (typeof value === 'object') {
node.children = this.buildfiletree(value,level + 1);
} else {
node.item = value;
}
}
return accumulator.concat(node);
},[]);
}
buildfiletree(TREE_DATA,0)
解决方法
简单
if (key === 'Almond Meal flour') {
return accumulator;
}