Search code examples
javascripttypescriptangular-materialangular-directiveangular-components

How to make function return reports based on report categoryID Paramter on service.ts?


I work on angular 7 app I need to return data from allReportsubCategory based on report Category ID

but I face issue how to return data from allReportsubCategory based on reportcategoryID .

so How to write function return reports based on ReportCategoryID ?

allReportsubCategory:any[];
this.allReportsubCategory =[
    {
        "reportID": 1,
        "reportName": "xxx",
        "reportIndex": 1,
        "reportCategoryID": 3,

    },
    {
        "reportID": 17,
        "reportName": "Jobs Monitor",
        "reportIndex": 1,
        "reportCategoryID": 3
    },
    {
        "reportID": 2025,
        "reportName": "Old Tables Report",
        "reportIndex": 1,
        "reportCategoryID": 3

    }];

on service.ts

 GetreportByCategoryId(reportCategoryID:string){
    return this.allReportsubCategory.??????

  }

so I need when pass 3 to function GetreportByCategoryId(reportcategoryID) return all data above based on reportcategoryid= 3

updated post

when call it on component as below

reportByCategoryId:any[]
 this.reportByCategoryId = this._displayreport.GetreportByCategoryId("3");
  console.log("data by category id" + this.reportByCategoryId )

no return for data above and console log as below :

data by category idundefined


Solution

  • To filter the values in array by some condition you can use filter method. Also you need to use number type in this method due to reportCategoryID in provided data has number type.

    GetreportByCategoryId(id: number) {
      return this.allReportsubCategory
        .filter(category => category.reportCategoryID === id);
    }