如何在service.ts上让函数根据报表categoryID Paramter返回报表?

问题描述 投票:0回答:1

我在angular 7应用上工作,需要根据报表category ID返回allReportsubCategory的数据。

但我面临的问题是如何根据reportcategoryID从allReportsubCategory返回数据。

所以如何写函数返回基于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

    }];

在service.ts上

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

  }

所以我需要当传递3到函数GetreportByCategoryId(reportcategoryID)时,根据reportcategoryid= 3返回上面所有数据。

更新后

在组件上调用它时,如下所示

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

以上数据没有返回,控制台日志如下。

数据按类别idundefined

javascript typescript angular-material angular-directive angular-components
1个回答
1
投票

要通过一些条件来过滤数组中的值,你可以使用以下方法 过滤法. 你还需要使用 number 在本方法中,由于 reportCategoryID 在所提供的数据中具有数字类型。

GetreportByCategoryId(id: number) {
  return this.allReportsubCategory
    .filter(category => category.reportCategoryID === id);
}
© www.soinside.com 2019 - 2024. All rights reserved.