【发布时间】:2019-06-05 14:52:10
【问题描述】:
我有 2 个数组:
- 从一天的特定开始时间到结束时间生成时间戳(每个
(1800 difference in each timestamp)30 分钟)。 - 我从 API 响应中得到一个预定的插槽响应。
我需要合并两个数组以形成代表可用和预定插槽的对象数组:
let availableTimeslots = [
1559709000,
1559710800,
1559712600,
1559714400,
1559716200,
1559718000,
1559719800,
1559721600,
1559723400,
1559725200,
1559727000,
1559728800,
1559730600,
1559732400,
1559734200,
1559736000,
1559737800,
1559739600
];
let bookedTimeSlots = {
bookings: [
{
timestamp: {
from: 1559719800,
to: 1559723400
}
},
{
timestamp: {
from: 1559730600,
to: 1559732400
}
}
]
};
我需要创建对象数组,例如:
[
{
available: true,
timeslots: [1559709000, 1559710800, 1559712600, 1559714400, 1559716200, 1559718000]
},
{
available: false,
timeslots: [1559719800, 1559721600, 1559723400]
},
{
available: true,
timeslots: [1559725200, 1559727000, 1559728800]
},
{
available: false,
timeslots: [1559730600, 1559732400]
},
{
available: true,
timeslots: [1559732400, 1559734200, 1559736000, 1559737800, 1559739600]
}
];
我真的很困惑如何继续这个
我正在考虑用所需的预定槽对象替换 availableTimeslots 中的值,然后用 {available: true, timeslots: [...]} 替换所有非对象值
bookedTimeSlots.bookings.map((bs, i)=> {
let ai = availableTimeslots.findIndex(bs.timestamp.from);
ai > -1 && (availableTimeslots[ai]={available: false, x : [..._.range(bs.timestamp.from, bs.timestamp.to, 1800)]});
})
任何帮助将不胜感激。
【问题讨论】:
-
to值在您的bookings[x].timestamp.to中是独占还是包含? -
@spender 它的包容性
标签: javascript arrays lodash chunks