首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >地图迭代角映射

地图迭代角映射
EN

Stack Overflow用户
提问于 2022-10-03 21:44:38
回答 1查看 107关注 0票数 0

我试图使用这个函数从数据源动态创建列,这个函数接受开始日期和结束日期,并创建列(例如: 2022-07到2022-10 ),我将创建4列: 2022-07、2022-08、2022-09、2022-10。

代码语言:javascript
复制
    Object.keys((this.statistiqueResults as AnimalMortParDestinationEtEspeceEtMois[])[0]?.animalMortParMois)
  .sort((a, b) => new Date(a).getTime() - new Date(b).getTime())
  .forEach((period) => {
    this.cols.push({
      field: 'nombreCorpsParDate',
      info: period,
      header: this.translate.instant('suivis.statistique.morts.nbAnimauxMortsMois', {
        period: this.datePipe.transform(new Date(period), 'MM/yyyy'),
      }),
      minWidth: 90,
    });
  });

问题是当我试图在这个迭代中使用键来迭代时。

代码语言:javascript
复制
this.statistiqueResults as AnimalMortParDestinationEtEspeceEtMois[])[0]?.animalMortParMois

我得到了“未定义”,这里有我正在使用的两个类和我的数据源的一个例子:

代码语言:javascript
复制
 export class AnimalMortParDestinationEtEspeceEtMois {
  animalMortParMois: Map<string, number>;
  animalMortParDestinationEtEspece: Map<string, AnimauxMortsDestinationsParEspece>;
}

 export class AnimauxMortsDestinationsParEspece {
  animalEspece: string;
  destinationMap: Map<string, Map<string, number>>;
  totalMortParDestination: Record<string, number>;
}

数据源(类型: AnimalMortParDestinationEtEspeceEtMois )

代码语言:javascript
复制
{
    "animalMortParMois": {
        "2022-03": 0,
        "2022-04": 0
    },
    "animalMortParDestinationEtEspeceDTO": {
        "porc": {
            "animalEspece": "porc",
            "destinationMap": {
                "INCINERATION_COLLECTIVE": {
                    "2022-03": 0,
                    "2022-04": 0
                },
                "EQUARRISSAGE": {
                    "2022-03": 0,
                    "2022-04": 0
                },
                "INCINERATION_INDIVIDUELLE": {
                    "2022-03": 0,
                    "2022-04": 0
                },
                "RECUPERE": {
                    "2022-03": 0,
                    "2022-04": 0
                },
                "NON_PRECISEE": {
                    "2022-03": 0,
                    "2022-04": 0
                }
            },
            "totalMortParDestination": {
                "INCINERATION_COLLECTIVE": 0,
                "INCINERATION_INDIVIDUELLE": 0,
                "RECUPERE": 0,
                "NON_PRECISEE": 0,
                "EQUARRISSAGE": 0
            }
        },
        "poisson": {
            "animalEspece": "poisson",
            "destinationMap": {
                "INCINERATION_COLLECTIVE": {
                    "2022-03": 0,
                    "2022-04": 0
                },
                "EQUARRISSAGE": {
                    "2022-03": 0,
                    "2022-04": 0
                },
                "INCINERATION_INDIVIDUELLE": {
                    "2022-03": 0,
                    "2022-04": 0
                },
                "RECUPERE": {
                    "2022-03": 0,
                    "2022-04": 0
                },
                "NON_PRECISEE": {
                    "2022-03": 0,
                    "2022-04": 0
                }
            },
            "totalMortParDestination": {
                "INCINERATION_COLLECTIVE": 0,
                "INCINERATION_INDIVIDUELLE": 0,
                "RECUPERE": 0,
                "NON_PRECISEE": 0,
                "EQUARRISSAGE": 0
            }
        }
    }
}

我需要计算的信息是:

代码语言:javascript
复制
  "animalMortParMois": {
        "2022-03": 0,
        "2022-04": 0
    },
EN

回答 1

Stack Overflow用户

发布于 2022-10-04 13:48:54

通过以下方式将数据解析为一个对象,从而得出了

代码语言:javascript
复制
  private initColsForAnimauxMorts(data: AnimalMortParDestinationEtEspeceEtMois): PrimeColumns[] {
    this.cols = this.getAnimalMortsCol();
    Object.keys(data.animalMortParMois)
      .sort((a, b) => new Date(a).getTime() - new Date(b).getTime())
      .forEach((period) => {
        this.cols.push({
          field: 'nombreCorpsParDate',
          info: period,
          header: this.translate.instant('suivis.statistique.morts.nbAnimauxMortsMois', {
            period: this.datePipe.transform(new Date(period), 'MM/yyyy'),
          }),
          minWidth: 90,
        });
      });
   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73941120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档