需要将日期类型的数组从字符串转换为日期。
下面是输入:
medicalData = [
{
name: 'Allergy',
status: 'Normal',
testDates: ['2023-07-02T13:21:29.643Z', '2023-07-03T13:21:29.644Z']
},
{
name: 'Asthma',
status: 'Deficient',
testDates: ['2023-08-02T13:21:29.643Z', '2023-08-03T13:21:29.644Z']
}
];
字符串
medical.service.ts:
const result = await this.medicalRecordRepository.create({
medicalData: medicalData // need to know how to use new Date() here
});
型
medical.schema.ts:
class MedicalData {
@Prop()
name: string;
@Prop()
status: string;
@Prop()
testDates: [Date];
}
export class MedicalRecord extends Document {
@Prop()
medicalData: MedicalData[];
}
型
我需要的testDates被保存在数据库中的日期不是日期字符串数组数组。
需要一些有价值的帮助
1条答案
按热度按时间9avjhtql1#
一种可行的方法是将医疗数据
map
两次map
,它使用参数的解构和rest属性,以及spread syntax,用于创建该数据条目的副本map
,它创建每个数据条目的testDates
数组的另一个变体,其中后者的项目是Date
示例...字符串
备注:
上面stack-snippet的SO特定日志记录将每个
Date
示例呈现为其自身的字符串化版本。浏览器的控制台日志记录将显示一个带有日期示例的数据结构。