ItFreak
ItFreak

Reputation: 2369

Angular filter objects array for multiple values

I have the following method that filters an object (that has an array of other objects) for an input field:

  filterArray(): void {
    this.json.items.filter((e) => e.assignedPool != null || e.assignedPool !== undefined);
    this.json.items = this.json.items.filter((e) => e.name.includes(this.filter));
  }

Now I want to filter for another variable. Here is the objecs hierarchie:

the filtered json variable:

export class VirtualServerResponse {
  private _kind: String;
  private _items: VirtualServer[];  

a VirtualServer:

  //...properties
  private _assignedPool: PoolDTO;

a Pool:

  //properties
  private _membersReference: MembersReferenceDTO;

the membersReference:

export class MembersReferenceDTO {
  constructor() {}
  private _link: String;
  private _isSubcollection: String;
  private _items: PoolMemberDTO[];

and a single PoolMember:

export class PoolMemberDTO {
  constructor() {}
  private _address: String;

Now I want to be able to filter also for the address of a PoolMember, so when the filter matches an address of any PoolMember, something like

this.json.items.foreach((item) => 
item.assignedPool.membersReference.items.foreach(member) =>  
member.address == this.filter)

I want to filter the json.items array, so this array contains:

Can someone help me here?

Update:

Server name: test server
Assigned pool -> membersReference ->items[]
items[0]: 10.10.10.2
items[1]: 10.10.10.3

... So if the filter is 10.10.10.3, the server 'test server' should be in json.items

Upvotes: 0

Views: 6639

Answers (1)

Eric Svitok
Eric Svitok

Reputation: 842

We can check memberReference items to see if there is an address that matches the filter.

filterArray(): void {
  this.json.items = this.json.items.filter((e) => 
    (e.assignedPool !== null && e.assignedPool !== undefined)
    && (e.name.includes(this.filter) || e.assignedPool.membersReference.items.find((m) => m.address === this.filter));
}

Upvotes: 2

Related Questions