简体   繁体   English

使用不同类型的 object Angular 从数组中过滤数据

[英]Filter data from array with a different type of object Angular

I get input data in this form:我以这种形式获得输入数据:

 [
    { name: "Producent", checked: true },
    { name: "Handel", checked: true },
    { name: "Simple", checked: true }
  ];

Only the checked values from true to false and vice versa can change.只有从真到假的检查值可以改变,反之亦然。 This is assigned to the checkedTypeOfClient variable.这被分配给 checkedTypeOfClient 变量。 Later, I'd like to filter out all my clients (the currentClientList array) based on the checkedTypeOfClient variable.稍后,我想根据 checkedTypeOfClient 变量过滤掉我的所有客户端(currentClientList 数组)。

Here are the properties of the Client class:以下是客户端 class 的属性:

export class Client {
    clientId: number;
    name: string ;
    district: string;
    province: string;
    zip: string;
    city: string;
    // tslint:disable-next-line: variable-name
    full_Address: string;
    latitude: number;
    longitude: number;
    segment: string;
    ph: string;
    bh: number;
    canal: string;
    isSimple: string;
}

The complication of this task is that the filtration goes like this.这项任务的复杂之处在于过滤过程是这样的。 The values Producent and Handel are values that can be placed in the canal column that are in the Client Class, and the Simple value is a value that is also in the Client class in the isSimple column and can take the value "YES" or "NO" Producent 和 Handel 的值是可以放在客户端 Class 中的 canal 列中的值,Simple 值是也在客户端 class 中的 isSimple 列中的值,可以取值“YES”或“不”

for now, what I was able to do is extract what values Producent, Handel, Simple are marked and grind the Simple field to "TAK" or NIE "现在,我能做的是提取标记了 Producent、Handel、Simple 的值,并将 Simple 字段研磨为“TAK”或 NIE“

filterClients() {
    console.log(this.checkedTypeOfClient);

    const filter={
      Producent:this.checkedTypeOfClient.find(x=>x.name=="Producent").checked,
      Handel:this.checkedTypeOfClient.find(x=>x.name=="Handel").checked,
      Simple:this.checkedTypeOfClient.find(x=>x.name=="Simple").checked
   }

     let simpleFilter = this.returnFilterSimpleValue(filter.Simple);

    this.currentClientList = this.baseClientList;

  }

  returnFilterSimpleValue(value : boolean) : string {

    switch (value) {
      case true:
          return "TAK";
      case false:
          return "NIE";
    }


If(Producent = True){
this.currentClientList(client.producent)
}
If(Handel= True){
this.currentClientList(client.handel)
}
If(Handel= True || Producent = True){
this.currentClientList(client.handel) && this.currentClientList(client.producent)
}

The question is how to filter it?问题是如何过滤它?

I'm not sure that I'm fully understanding your question.我不确定我是否完全理解你的问题。 However, here's how I'd solve it.但是,这就是我要解决的方法。

First of all, I'd like to introduce you to union types (a feature of typescript that allows you to combine multiple types, but can also be used for literal values .首先,我想向您介绍联合类型(typescript 的一个功能,允许您组合多种类型,但也可以用于文字值

In your case that would be useful for:在您的情况下,这将有助于:

  • isSimple: "YES" | "NO" isSimple: "YES" | "NO" meaning that it can only have either values. isSimple: "YES" | "NO"意味着它只能有任何一个值。 Also, why not make it a boolean?另外,为什么不让它成为 boolean?
  • canal: "Handel" | "Producent" canal: "Handel" | "Producent" meaning that it can only have either the "Handel" or "Producent" string value. canal: "Handel" | "Producent"意味着它只能具有“Handel”或“Producent”字符串值。

Secondly you can simple use filter array method to filter the objects that have a certain property values, which can also be chained.其次,您可以简单地使用filter数组方法过滤具有特定属性值的对象,也可以将其链接起来。

Is this what you wanted to do or did I miss something?这是你想做的还是我错过了什么?

I need value:我需要价值:

If(Producent = True){
this.currentClientList(client.producent)
}
If(Handel= True){
this.currentClientList(client.handel)
}
If(Handel= True || Producent = True){
this.currentClientList(client.handel) && this.currentClientList(client.producent)
}

I'm not sure either if I get the question right.我也不确定我的问题是否正确。 As i understand it, you have two different types of clients: CanalClient and SimpleClient .据我了解,您有两种不同类型的客户端: CanalClientSimpleClient with the superclass Client sharing the common attributes.与超类Client共享公共属性。

export class CanalClient extends Client {
canal: string;
}

export class Simpleclientextends Client {
isSimple: string; // as Ruben asked - why is this not a simple boolean?
}

In the filter Operation, you can then check the class with instanceof CanalClient etc. to have type safety.在过滤器操作中,您可以使用instanceof CanalClient等检查 class 是否具有类型安全性。 Actually, in an if block- this is type guarded:实际上,在 if 块中,这是类型保护的:

if(client instanceof CanalClient) {
console.log( client.canal) // Typescript will know this is of type Canalclient!
}

I tried to keep it as simple as I could by reusing your code as per my understanding.我试图通过根据我的理解重用您的代码来使其尽可能简单。 Try this:尝试这个:

filterClients() {
    console.log(this.checkedTypeOfClient);

    const filter={
        Producent:this.checkedTypeOfClient.find(x=>x.name=="Producent").checked,
        Handel:this.checkedTypeOfClient.find(x=>x.name=="Handel").checked,
        Simple:this.checkedTypeOfClient.find(x=>x.name=="Simple").checked
    }

   let simpleFilter = this.returnFilterSimpleValue(filter.Simple);

   this.currentClientList = this.baseClientList.filter(c => {
       if (c.canal=='Producent' && filter.Producent) {
         return true;
       }
       if (c.canal=='Handel' && filter.Handel) {
         return true;
       }
    });
    this.currentClientList.foEach(c => { c.isSimple = this.returnFilterSimpleValue(c.isSimple) });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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