如何以角度4过滤控制器中的数据

时间:2017-11-28 09:35:27

标签: angular

我有以下功能

getMyList(id:number){
  this.subCatSvs.getList(id)
    .subscribe(List => {
       this.List = List;
     });
};

此功能的输出为:

[
{
"id": 678,
"name": "Bench",
"type": "furniture",
},
{
"id": 679,
"name": "Chair",
"type": "furniture",

},
{
"id": 677,
"name": "Stool",
"type": "furniture",

},
{
"id": 679,
"name": "Chair",
"type": "furniture",

},
{
"id": 680,
"name": "Car",
"type": "Vehicle",

},
{
"id": 681,
"name": "Van",
"type": "Vehicle",

},
]

如何修改我的功能以便能够按"类型"?过滤日期?例如,我只想让我的输出显示车辆。谢谢你的帮助。

AJ

3 个答案:

答案 0 :(得分:2)

使用array.filter

this.List = List.filter(t=>t.type ==='Vehicle');

答案 1 :(得分:2)

您可以使用Array.prototype.filter()来过滤数组。

以下内容应该有效:

this.List = this.List.filter(item => item.type === 'Vehicle');

答案 2 :(得分:0)

    transform(items: Incident[], location_id: string, driver_id: string,
        description: string, type: string,technician_id: string){
        if (items && items.length){
            return items.filter(item =>{
                if (location_id && item.location_id.name.toLowerCase().indexOf(location_id.toLowerCase()) === -1){
                    return false;
                }
                if (driver_id && item.driver_id.name.toLowerCase().indexOf(driver_id.toLowerCase()) === -1){
                    return false;
                }
                if (description && item.description.toLowerCase().indexOf(description.toLowerCase()) === -1){
                  return false;
                }
                if (type && item.incidenttype_id.name.toLowerCase().indexOf(type.toLowerCase()) === -1){
                  return false;
                }
                if (technician_id && item.technician_id.name.toLowerCase().indexOf(technician_id.toLowerCase()) === -1){
                    return false;
                }`enter code here`

                return true;
           })
        }
        else{
            return items;
        }
    }
 in HTML
<tr *ngFor="let Incident of Incidents | filter: location_id : driver_id : description : type : technician_id : startdate" >