我有一些这样的模型
export class ExpertOperational {
comment: string;
outOfService: Date;
score: string;
supplierNumber: string;
useSlots: boolean;
workingDay: ExpertWorkingDay[] = [];
workSpeedSurvey: string;
workSpeedVisit: string;
}
export class ExpertWorkingDay {
constructor(
public weekDay: WeekDays,
public operationalAM: ExpertWorkingHours,
public operationalPM: ExpertWorkingHours
) {}
}
export enum WeekDays {
MONDAY = 'MONDAY',
TUESDAY = 'TUESDAY',
WEDNESDAY = 'WEDNESDAY',
THURSDAY = 'THURSDAY',
FRIDAY = 'FRIDAY',
SATURDAY = 'SATURDAY',
SUNDAY = 'SUNDAY'
}
export class ExpertWorkingHours {
constructor(
public fixed: boolean,
public startTime: string,
public endTime: string
) {}
}
我需要的是从枚举和模型中创建空对象
const expert = {
comment: '',
outOfService: '',
score: '',
supplierNumber: '',
useSlots: false,
workingDay: [
{
weekDay: 'MONDAY',
operationalAM: {
fixed: false,
startTime: '',
endTime: ''
},
operationalPM: {
fixed: false,
startTime: '',
endTime: ''
}
},
{
weekDay: 'TUESDAY',
operationalAM: {
fixed: false,
startTime: '',
endTime: ''
},
operationalPM: {
fixed: false,
startTime: '',
endTime: ''
}
},
{
weekDay: 'WEDNESDAY',
operationalAM: {
fixed: false,
startTime: '',
endTime: ''
},
operationalPM: {
fixed: false,
startTime: '',
endTime: ''
}
},
{
weekDay: 'THURSDAY',
operationalAM : {
fixed: false,
startTime: '',
endTime: ''
},
operationalPM
: {
fixed: false,
startTime: '',
endTime: ''
}
},
{
weekDay: 'FRIDAY',
operationalAM: {
fixed: false,
startTime: '',
endTime: ''
},
operationalPM: {
fixed: false,
startTime: '',
endTime: ''
}
},
{
weekDay: 'SATURDAY',
operationalAM: {
fixed: false,
startTime: '',
endTime: ''
},
operationalPM: {
fixed: false,
startTime: '',
endTime: ''
}
}, {
weekDay: 'SUNDAY',
operationalAM: {
fixed: false,
startTime: '',
endTime: '',
},
operationalPM: {
fixed: false,
startTime: '',
endTime: ''
}
}
],
workSpeedSurvey: '',
workSpeedVisit: ''
}
我需要的是用模型中的空值创建对象,我不需要数据! 我已经尝试过了,但是我没有运气
const data: ExpertWorkingDay[] = [];
const workingHours = Object.values(WeekDays);
workingHours.forEach(day => {
data.push(
new ExpertWorkingDay(
WeekDays[day],
new ExpertWorkingHours(
fixed: false,
startTime = '',
endTime = ''
),
new ExpertWorkingHours(
fixed: false,
startTime = '',
endTime = ''
)
)
);
});
谢谢
答案 0 :(得分:1)
由于您的day
是动态的,因此您必须指定day
的类型为WeekDays
。
为此,您可以使用WeekDays[day as keyof WeekDays]
下面的代码将帮助您:
workingHours.forEach(day => {
data.push(
new ExpertWorkingDay(
WeekDays[day as keyof WeekDays],
new ExpertWorkingHours(
false,
'',
''
),
new ExpertWorkingHours(
false,
'',
''
)
)
);
});
我已经修改了同一个例子,但有手头的问题。请参阅this。
我已将workingDays
添加为['MONDAY' , 'TUESDAY']
。
编辑:
由于OP尝试使用Object.values()
获取枚举键,所以您无需执行任何操作,因为Object.values()
将返回Array<WeekDays>
。因此,将const workingHours
的类型设置为Array<WeekDays>
。
const data: ExpertWorkingDay[] = [];
// const workingHours : string[] = Object.keys(WeekDays); //If enum keys
// const workingHours : string[] = ['MONDAY' , 'TUESDAY']; //if day as array
const workingHours : WeekDays[] = Object.values(WeekDays);
workingHours.forEach(day => {
data.push(
new ExpertWorkingDay(
WeekDays[day],
new ExpertWorkingHours(
false,
'',
''
),
new ExpertWorkingHours(
false,
'',
''
)
)
);
});
console.log(data);
答案 1 :(得分:0)
如果我正确理解它,则不希望使用数组的硬编码值,而应使用枚举。
如果有帮助,我添加了示例代码。
const data: ExpertWorkingDay[] = [];
const workingHours = Object.keys(WeekDays);
console.log(workingHours);
workingHours.forEach(day => {
data.push(
new ExpertWorkingDay(
WeekDays[day as keyof WeekDays],
new ExpertWorkingHours(
false,
'',
''
),
new ExpertWorkingHours(
false,
'',
''
)
)
);
});