If you decide to adjust the response
array slots to match the body format, you can use a straightforward method like this:
Here's a simple and easy-to-understand approach:
let result = [...body];
for (let time of body) {
let responseSlot = response.find(s => s.slot == time.slot);
if (responseSlot) {
result = [...result.filter(t => t != time), responseSlot];
}
}
This method will generate the following output:
[
{ slot: '9:30',
status: 'Available',
clientName: '',
doctorName: ''
},
{ slot: '1:30',
status: 'Available',
clientName: '',
doctorName: ''
},
{
clientName: 'John Doe',
doctorName: 'Paul Pierce',
slot: '9:00',
status: 'Not Available'
},
{
clientName: 'James Bond',
doctorName: 'Chris Paul',
slot: '1:00',
status: 'Not Available'
}
]
In this scenario, update the response array to this:
const response = [{
clientName: "John Doe",
doctorName: "Paul Pierce",
slot: "9:00",
status: "Not Available"
}, {
clientName: "James Bond",
doctorName: "Chris Paul",
slot: "1:00",
status: "Not Available"
}]