我试图使用这个函数从数据源动态创建列,这个函数接受开始日期和结束日期,并创建列(例如: 2022-07到2022-10 ),我将创建4列: 2022-07、2022-08、2022-09、2022-10。
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,
});
});问题是当我试图在这个迭代中使用键来迭代时。
this.statistiqueResults as AnimalMortParDestinationEtEspeceEtMois[])[0]?.animalMortParMois我得到了“未定义”,这里有我正在使用的两个类和我的数据源的一个例子:
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 )
{
"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
}
}
}
}我需要计算的信息是:
"animalMortParMois": {
"2022-03": 0,
"2022-04": 0
},发布于 2022-10-04 13:48:54
通过以下方式将数据解析为一个对象,从而得出了:
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,
});
});
}https://stackoverflow.com/questions/73941120
复制相似问题