Q:

Angular 2 pipe to filter grouped arrays

I have a group of arrays on my Angular2 app that I use to build a grouped list with *ngFor in my view:
[
  {
    category: 1,
    items: [{ id: 1, name: "helloworld1" }, { id: 2, name: "helloworld2" }]
  },
  {
    category: 2,
    items: [{ id: 3, name: "helloworld3" }, { id: 4 }]
  },
  {
    category: 3,
    items:[{ id: 5 }, { id: 6 }]
  }
]

I also have a boolean that when it's true should filter only the items that have the name property. If a group does not have any item that matches this condition it should not pass. So the result would be the following if the boolean is true:
[
  {
    category: 1,
    items: [{ id: 1, name: "helloworld1" }, { id: 2, name: "helloworld2" }]
  },
  {
    category: 2,
    items: [{ id: 3, name: "helloworld3" }]
  }
]

How can I implement a pipe to achieve this kind of result?

A:

http://plnkr.co/edit/je2RioK9pfKxiZg7ljVg?p=preview
@Pipe({name: 'filterName'})
export class FilterNamePipe implements PipeTransform {
transform(items: any[], checkName: boolean): number {
  if(items === null) return [];
  let ret = [];
  items.forEach(function (item) {
    let ret1 = item.items.filter(function (e) {
      return !checkName || (checkName && (e.name !== undefined));
    });
    if(ret1.length > 0) {
      item.items = ret1;
      ret.push(item);
    }
    });
  return ret;
  }
}

