У меня есть ниже массив JSON.Я хочу установить расписание для dayOfWeek
, которого нет внутри schedules
const dummy = { home1: "09:30", away: "05:30" }
const data = {
rooms: [
{
roomId: 1,
schedules: [
{ home1: "05:05", dayOfWeek: 1, away: "20:30" },
{ home1: "06:05", dayOfWeek: 5, away: "21:30" },
{ home1: "07:05", dayOfWeek: 7, away: "22:30" }
]
},
{
roomId: 2,
schedules: []
}
]
}
Теперь я немного запутался.Мне нужно скопировать те же schedules
в room: 2
, которые находятся в room: 1
и которых нет. Я хочу установить пустышку с dayOfWeek
ожидаемым выводом
const finalArray = [
//room: 1
{ home1: "09:30", dayOfWeek: 2, away: "05:30", roomId: 1 }, //dummy
{ home1: "09:30", dayOfWeek: 3, away: "05:30", roomId: 1 }, //dummy
{ home1: "09:30", dayOfWeek: 4, away: "05:30", roomId: 1 }, //dummy
{ home1: "09:30", dayOfWeek: 6, away: "05:30", roomId: 1 } //dummy
//room: 2
{ home1: "05:05", dayOfWeek: 1, away: "20:30", roomId: 2 }, //schedules from room1
{ home1: "06:05", dayOfWeek: 5, away: "21:30", roomId: 2 }, //schedules from room1
{ home1: "07:05", dayOfWeek: 7, away: "22:30", roomId: 2 }, //schedules from room1
{ home1: "09:30", dayOfWeek: 2, away: "05:30", roomId: 2 }, //dummy
{ home1: "09:30", dayOfWeek: 3, away: "05:30", roomId: 2 },//dummy
{ home1: "09:30", dayOfWeek: 4, away: "05:30", roomId: 2 },//dummy
{ home1: "09:30", dayOfWeek: 6, away: "05:30", roomId: 2 }//dummy
]
Что я пробовал
Я могу установить фиктивные данные из первого элемента расписаний.Но не знаю, как проверить каждый элемент в массиве rooms.schedules
const dummy = _.first(data.rooms[0].schedules) ? _.first(rooms[0].schedules) : { 'home1': '05:30', 'home2': '09:30', 'away': '17:30', 'sleep': '20:30' }
const finalArray = []
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)) finalArray.push({ ...obj, dayOfWeek: i, id: room.id })
}
}