简体   繁体   中英

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:

  • all servers with name matching the filter
  • all servers which assignedPool members addresses match the filter

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM