Угловой редуктор и аккумулятор с Null

В следующем коде я хочу сделать что-то вроде

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)

1 ответ

Its easy

if (key === 'Almond Meal flour') {
        return accumulator;
}
Другие вопросы по тегам