I have below json
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" }
]
}
]
}
Now I need to push the above elements for the dayOfWeek which are not present inside the schedules
array of both the rooms
This is the output I want
const finalOuput = [
//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 have tried loop over the rooms
array something like This
const finalOuput = []
rooms.map((room) => {
room.schedules.map((schedule) => {
finalOuput.push(schedule)
})
})
But don't know how to check for the dayOfWeek
which are not present inside the rooms
schedules.
Can someone please help to acheive this. Thank you!!!
ES6 only solution:
const data = { rooms: [{ roomId: 1, schedules: [{ home1: "06:00", dayOfWeek: 1, away: "21:30", roomId: 1 }, { home1: "06:05", dayOfWeek: 2, away: "22:30", roomId: 1 } ] }, { roomId: 2, schedules: [{ home1: "06:00", dayOfWeek: 4, away: "21:30", roomId: 2 }, { home1: "06:05", dayOfWeek: 5, away: "22:30", roomId: 2 } ] } ] }
const getSchedules = (room) => {
let weekDays = [...Array(8).keys()]
weekDays.shift()
let days = weekDays.filter(x => !room.schedules.some(y => y.dayOfWeek == x))
return days.map(y => ({ home1: "00:00", dayOfWeek: y, away: "02:30", roomId: room.roomId }))
}
console.log(data.rooms.reduce((r,c) => (r.push(...getSchedules(c)), r), []))
Lodash version:
const data = { rooms: [{ roomId: 1, schedules: [{ home1: "06:00", dayOfWeek: 1, away: "21:30", roomId: 1 }, { home1: "06:05", dayOfWeek: 2, away: "22:30", roomId: 1 } ] }, { roomId: 2, schedules: [{ home1: "06:00", dayOfWeek: 4, away: "21:30", roomId: 2 }, { home1: "06:05", dayOfWeek: 5, away: "22:30", roomId: 2 } ] } ] }
const getSchedules = (room) => {
let days = _.difference(_.range(1,8), _.map(room.schedules, 'dayOfWeek'))
return days.map(y => ({ home1: "00:00", dayOfWeek: y, away: "02:30", roomId: room.roomId }))
}
console.log(_.reduce(data.rooms, (r,c) => (r.push(...getSchedules(c)), r), []))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
The idea is to use the the difference between the range of 1...7
and the current days in each room.schedule
via (_.difference
& _.range
in lodash and Array.filter
in ES6) and just hydrate the result in the resulting output.