js 数组转换问题?

根据会议预定记录(一维数组)制作会议预定表

已知会议室列表:

['201', '302', '403']

已知每天四个固定时间段:

['9-11', '11-14', '14-16', '16-18']

已知最近三天(不含周末)的会议预定记录:

[
  { date: '2020-06-29', time: '9-11', room: '201', unit: 'my company 1' },
  { date: '2020-06-30', time: '11-14', room: '302', unit: 'my company 2' }
]

要求是生成最近三天(不含周末)的会议预定表,格式如下:

[
    {
        "date":"2020-06-29",
        "week":"周一",
        "rooms":[
            {
                "name":"201",
                "meetings":[
                    {
                        "time":"9-11",
                        "unit":"my company 1"
                    },
                    {
                        "time":"11-14",
                        "unit":""
                    },
                    {
                        "time":"14-16",
                        "unit":""
                    },
                    {
                        "time":"16-18",
                        "unit":""
                    }
                ]
            },
            {
                "name":"302",
                "meetings":[
                    {
                        "time":"9-11",
                        "unit":""
                    },
                    {
                        "time":"11-14",
                        "unit":"my company 2"
                    },
                    {
                        "time":"14-16",
                        "unit":""
                    },
                    {
                        "time":"16-18",
                        "unit":""
                    }
                ]
            },
            {
                "name":"403",
                "meetings":[
                    {
                        "time":"9-11",
                        "unit":""
                    },
                    {
                        "time":"11-14",
                        "unit":""
                    },
                    {
                        "time":"14-16",
                        "unit":""
                    },
                    {
                        "time":"16-18",
                        "unit":""
                    }
                ]
            }
        ]
    },
    {// 此处是2020-06-30的数据,格式同上},
    {// 此处是2020-07-01的数据,格式同上}
]
阅读 1.4k
1 个回答
const meetingRooms = ['201', '302', '403'];
const timeSlot = ['9-11', '11-14', '14-16', '16-18'];
const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
const bookingRecord = [
    { date: '2020-06-29', time: '9-11', room: '201', unit: 'my company 1' },
    { date: '2020-06-29', time: '9-11', room: '302', unit: 'my company 2' },
    { date: '2020-06-29', time: '11-14', room: '201', unit: 'my company 3' },
    { date: '2020-06-30', time: '11-14', room: '302', unit: 'my company 4' },
];

let result = {};

const initialMeetings = timeSlot.map(time => ({ time, unit: '' }));
const initialRooms = meetingRooms.map(room => ({ name: room, meetings: initialMeetings }));

bookingRecord.forEach(({ date, time, room, unit }) => {
    if (!(date in result)) {
        result[date] = {
            date,
            week: weekday[new Date(date).getDay()],
            rooms: JSON.parse(JSON.stringify(initialRooms)),
        };
    }

    result[date].rooms
    .find(({ name }) => name === room).meetings
    .find(({ time: t }) => t === time).unit = unit;
});

result = Object.values(result);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题