I have the following JSON data
const data = {
rooms: [
{
roomId: 1,
schedules: [
{ home1: "06:00", dayOfWeek: 1, away: "21:30" },
{ home1: "06:05", dayOfWeek: 2, away: "22:30" }
]
},
{
roomId: 2,
schedules: [
{ home1: "06:00", dayOfWeek: 4, away: "21:30" },
{ home1: "06:05", dayOfWeek: 5, away: "22:30" }
]
}
]
}
My goal is to add elements for the missing dayOfWeek
within the schedules
array of both rooms.
This is the desired output:
const finalOutput = [
//for room 1
{ home1: "00:00", dayOfWeek: 3, away: "02:30", roomId: 1 },
{ home1: "00:00", dayOfWeek: 4, away: "02:30", roomId: 1 },
{ home1: "00:00", dayOfWeek: 5, away: "02:30", roomId: 1 },
{ home1: "00:00", dayOfWeek: 6, away: "02:30", roomId: 1 },
{ home1: "00:00", dayOfWeek: 7, away: "02:30", roomId: 1 },
//for room 2
{ home1: "00:00", dayOfWeek: 1, away: "02:30", roomId: 2 },
{ home1: "00:00", dayOfWeek: 2, away: "02:30", roomId: 2 },
{ home1: "00:00", dayOfWeek: 3, away: "02:30", roomId: 2 },
{ home1: "00:00", dayOfWeek: 6, away: "02:30", roomId: 2 },
{ home1: "00:00", dayOfWeek: 7, away: "02:30", roomId: 2 },
]
I attempted to loop over the rooms
array as follows
const finalOutput = []
rooms.map((room) => {
room.schedules.map((schedule) => {
finalOutput.push(schedule)
})
})
However, I am unsure how to identify and add the missing dayOfWeek
values. Any assistance would be greatly appreciated. Thank you!