Reputation: 643
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!!!
Upvotes: 1
Views: 73
Reputation: 19070
You can use Array.prototype.reduce() combined with Array.prototype.concat(), Array.prototype.filter(), Array.prototype.find() and Array.prototype.map()
Code:
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" }]}]}
const finalOuput = data.rooms.reduce((a, c) => a.concat(
[1, 2, 3, 4, 5, 6, 7]
.filter(d => !c.schedules.find(s => s.dayOfWeek === d))
.map(availableDay => ({
roomId: c.roomId,
home1: '00:00',
dayOfWeek: availableDay,
away: '02:30'
}))
), []);
console.log(finalOuput);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 1525
Here's the code
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" }
]
}
]
}
let output = []
for (let room of data.rooms) {
let days = []
room.schedules.map(s => days.push(parseInt(s.dayOfWeek)))
days = new Set(days)
for(let i = 1; i <= 7; i++) {
if(!days.has(i)) output.push({ 'home1': '00:00', 'dayOfWeek': i, 'away': '02:30', 'roomId': room.roomId })
}
}
console.log(output)
Upvotes: 1
Reputation: 18515
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.
Upvotes: 2
Reputation: 92440
You can create an array all the days of the week and filter if by whether that day exists in you schedules
array.
Then map over the filtered array and build your objects:
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" }]}]}
const days = [1, 2, 3, 4, 5, 6, 7]
const template = { home1: "00:00", away: "02:30", }
const rooms = data.rooms.reduce((arr, {roomId, schedules}) => {
// missing is the days no presesnt in schedules
let missing = days.filter(day => !schedules.find(s => s.dayOfWeek == day ))
return arr.concat( ... missing.map(d => Object.assign({}, template, {dayOfWeek: d,room: roomId})))
}, [])
console.log(rooms)
Upvotes: 2