实现如下函数 start,和end 为开始时间和结束时间 type 为 day, month, year remote是远端数据 数据结构为 [{time: '2020-02-02'}],且time在start和end时间范围内 要求:返回一个数组,根据type补全时间 const fillTime = (start, end, remote, type): [] => {}fillTime('2020-01', '2020-03', [{time: '2020-02'}], 'month')
时间: 2023-03-07 12:30:11 浏览: 181
答案:可以使用以下函数来实现:const fillTime = (start, end, remote, type) => { const startTime = new Date(start); const endTime = new Date(end); const result = []; while (startTime <= endTime) { const currentTime = startTime.toISOString().slice(0, 10); const found = remote.find(item => item.time === currentTime); if (found) { result.push(found); } else { result.push({ time: currentTime }); } startTime.setMonth(startTime.getMonth() + 1); } return result; } fillTime(2020-01, 2020-03, [{time: 2020-02}], month);
阅读全文