Angular 2 pipe to filter grouped arrays

user1320990

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?

Yong

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;
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Angular 2 Pipe under condition

From Dev

Angular JS - Creating a filter to compare 2 arrays

From Dev

angular 2 date pipe weeknumber

From Dev

Angular 2 pipe/filter with component variable

From Dev

How to pass meta data about how to filter in pipe using Angular2?

From Dev

Dynamic pipe in Angular 2

From Dev

Angular 2 "time ago" pipe

From Dev

angular 2 Pipe - filter by JSON key

From Dev

Angular 2 Input Custom Pipe

From Dev

Angular2 Pipe filter by name best approach

From Dev

Filter object array with pipe Angular 2

From Dev

Angular 2 pipe/filter with component variable

From Dev

How do I filter a javascript object when making a search pipe with angular 2?

From Dev

angular2 case insensitive filter using pipe?

From Dev

angular 2 Pipe - filter by JSON key

From Dev

Angular2 filter pipe on input

From Dev

Find and filter pipe for all content of Array as JSON format Angular 2

From Dev

Angular 2 Search Pipe filter objects with array property

From Dev

Angular2 Filter Array of Objects with pipe using RegExp?

From Dev

Angular2 filter table based on two inputs using custom pipe

From Dev

Angular2: Error while using a text filter pipe to filter an array

From Dev

My custom pipe (filter) does not work in Angular 2

From Dev

angular2 - pipe - filter multiple json values

From Dev

How to create pipe to filter list in Angular2

From Dev

Angular2 pipe does filter but doesn't show at all

From Dev

Angular2 filter/pipe using radio buttons to filter

From Dev

Angular 5, Passing table of tags to pipe filter

From Dev

Angular filter pipe doesn´t work

From Dev

Angular Filter pipe not working properly

Related Related

HotTag

Archive