我正在尝试根据输入更新我的活动状态
我正在尝试这样我的代码
const doctorID = "DR001"
const startTime = "12:15"
const doctors = [{
doctorID: 'DR001',
FirstName: 'aman',
LastName: 'kumar',
Price: 640,
Currency: 'rs',
slotsTime: [{
active: true,
startTime: '12:00',
endTime: '12:15'
},
{
active: true,
startTime: '12:15',
endTime: '12:30'
},
{
active: true,
startTime: '16:00',
endTime: '16:15'
},
{
active: true,
startTime: '16:15',
endTime: '16:30'
},
],
},
{
doctorID: 'DR002',
FirstName: 'aman',
LastName: 'kumar',
Price: 640,
Currency: 'rs',
slotsTime: [{
active: true,
startTime: '10:00',
endTime: '10:15'
},
{
active: true,
startTime: '10:15',
endTime: '10:30'
},
{
active: true,
startTime: '14:00',
endTime: '14:15'
},
{
active: true,
startTime: '14:15',
endTime: '14:30'
},
],
},
];
// Find the doctor object with the matching doctorID
const doctor = doctors.find((doc) => doc.doctorID === doctorID);
// Update the slotsTime array based on the startTime input
doctor.slotsTime = doctor.slotsTime.map((slot) => {
if (slot.startTime === startTime) {
return {
active: false,
startTime: slot.startTime,
endTime: slot.endTime
};
} else {
return slot;
}
});
// Log the updated doctor object
console.log(doctor);
我出去放这个
{
doctorID: 'DR001',
FirstName: 'aman',
LastName: 'kumar',
Price: 640,
Currency: 'rs',
slotsTime: [
{ active: true, startTime: '12:00', endTime: '12:15' },
{ active: false, startTime: '12:15', endTime: '12:30' },
{ active: true, startTime: '16:00', endTime: '16:15' },
{ active: true, startTime: '16:15', endTime: '16:30' }
]
}
现在我想要如果我再次输入这个
const doctorID = "DR001"
const startTime = "12:00"
const doctors = [
{
doctorID: 'DR001',
FirstName: 'aman',
LastName: 'kumar',
Price: 640,
Currency: 'rs',
slotsTime: [
{ active: true, startTime: '12:00', endTime: '12:15' },
{ active: true, startTime: '12:15', endTime: '12:30' },
{ active: true, startTime: '16:00', endTime: '16:15' },
{ active: true, startTime: '16:15', endTime: '16:30' },
],
},
{
doctorID: 'DR002',
FirstName: 'aman',
LastName: 'kumar',
Price: 640,
Currency: 'rs',
slotsTime: [
{ active: true, startTime: '10:00', endTime: '10:15' },
{ active: true, startTime: '10:15', endTime: '10:30' },
{ active: true, startTime: '14:00', endTime: '14:15' },
{ active: true, startTime: '14:15', endTime: '14:30' },
],
},
];
然后我得到这样的输出
{
doctorID: 'DR001',
FirstName: 'aman',
LastName: 'kumar',
Price: 640,
Currency: 'rs',
slotsTime: [
{ active: false, startTime: '12:00', endTime: '12:15' },
{ active: false, startTime: '12:15', endTime: '12:30' },
{ active: true, startTime: '16:00', endTime: '16:15' },
{ active: true, startTime: '16:15', endTime: '16:30' }
]
}
我想当活动状态更新一次他总是假当下一个活动状态更新假然后得到状态假渗透和下一个
我不确定我是否完全理解你的问题,但似乎你想在返回之前对原始对象中的
active
属性进行更改,以便下次交互时它会在那里。
如果是这样,这就是你可以做的:
if (slot.startTime === startTime) {
slot.active = false
return slot;
} else {
return slot;
如果我没理解错的话,你要确保一旦某个插槽的活动状态设置为 false,即使其他插槽已更新,它仍然是 false。为此,您可以跟踪更新活动状态的函数之外的更改。尝试像小夹具这样的东西:
const doctorID = "DR001";
const startTime = "12:15";
const doctors = [
// ... Your doctor data here ...
];
// Function to update the doctor's slotsTime based on the doctorID and startTime input
function updateDoctorSlot(doctorID, startTime, doctors) {
// Find the doctor object with the matching doctorID
const doctor = doctors.find((doc) => doc.doctorID === doctorID);
// Update the slotsTime array based on the startTime input
doctor.slotsTime = doctor.slotsTime.map((slot) => {
if (slot.startTime === startTime) {
return {
active: false,
startTime: slot.startTime,
endTime: slot.endTime,
};
} else {
return slot;
}
});
return doctor;
}
const updatedDoctor1 = updateDoctorSlot(doctorID, startTime, doctors);
console.log(updatedDoctor1);
const doctorID2 = "DR001";
const startTime2 = "12:00";
const updatedDoctor2 = updateDoctorSlot(doctorID2, startTime2, doctors);
console.log(updatedDoctor2);
这样,您可以使用不同的 doctorID 和 startTime 值调用 updateDoctorSlot() 函数,它会相应地更新活动状态,同时保留之前的更改。