How to filter data in the controller in angular 4

Viewed 8751

I have the following function

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

The output of this function is:

[
{
"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",

},
]

How can I modify my function to be able to filter date by "type"? For example, I just want my output to show vehicle. Thanks for your help.

AJ

3 Answers
    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" >
Related