在下面的代码中,我想做类似的事情
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,
'Organic eggs': null,
'Protein Powder': null,
Fruits: {
Apple: null,
Berries: ['Blueberry', 'Raspberry'],
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)
答案 0 :(得分:0)
简单
if (key === 'Almond Meal flour') {
return accumulator;
}