我提出了一个概念,但是在将其转换为代码时遇到了麻烦。
这是一张更好地帮助解释我的想法的图片:
无法通过图片说明,但日期之间也可能存在差距。
不要让它成为XY问题,这就是我需要这样做的原因。我有一系列的日期范围和奖金,我想创建一系列不重叠的日期和奖金。
我试图将重叠分成不再重叠的夹头。遇到了一个名为moment-range的软件包。是否有一个名字可以帮助我成功? "将日期范围投射到线性...某事"
下面的最终代码
const _ = require('lodash')
const Moment = require('moment')
const MomentRange = require('moment-range')
const moment = MomentRange.extendMoment(Moment);
let bonuses = [
{start: new Date('Jan 15, 2018 23:00:00 GMT+0200'), end: new Date('Jan 18, 2018 23:59:59 GMT+0200'), bonus: 30, preSale: true},
{start: new Date('Jan 17, 2018 00:00:00 GMT+0200'), end: new Date('Jan 29, 2018 13:00:00 GMT+0200'), bonus: 25, preSale: true},
{start: new Date('Feb 12, 2018 00:00:00 GMT+0200'), end: new Date('Feb 18, 2018 23:59:59 GMT+0200'), bonus: 20, preSale: false},
{start: new Date('Feb 19, 2018 00:00:00 GMT+0200'), end: new Date('Feb 27, 2018 23:59:59 GMT+0200'), bonus: 15, preSale: false},
{start: new Date('Feb 26, 2018 00:00:00 GMT+0200'), end: new Date('Mar 4, 2018 23:59:59 GMT+0200'), bonus: 10, preSale: false},
{start: new Date('Mar 5, 2018 00:00:00 GMT+0200'), end: new Date('Mar 11, 2018 23:59:59 GMT+0200'), bonus: 5, preSale: false},
]
_.map(bonuses, o => o.range = moment.range(o.start, o.end))
let dates = []
_.each(bonuses, o => {
dates.push(o.start)
dates.push(o.end)
})
dates.sort(function(a, b) {
return a-b
})
let ranges = []
for(let i=1; i<dates.length; i++) {
ranges.push({
start: dates[i-1],
end: dates[i],
range: moment.range(dates[i-1], dates[i])
})
}
for (let range of ranges) {
range.bonus = 0
for (let bonus of bonuses) {
if (range.range.intersect(bonus.range)) {
range.bonus += bonus.bonus
}
}
}
_.each(ranges, r => console.log(r.start, ' - ', r.end, ' => ', r.bonus))
答案 0 :(得分:0)
这样的东西?
var min = Infinity;
var max = 0;
var bonuses = [
{ start: new Date('Jan 15, 2018 23:00:00 GMT+0200'), end: new Date('Jan 18, 2018 23:59:59 GMT+0200'), bonus: 30, intersects: false },
{ start: new Date('Jan 16, 2018 00:00:00 GMT+0200'), end: new Date('Jan 29, 2018 13:00:00 GMT+0200'), bonus: 25, intersects: false },
{ start: new Date('Feb 12, 2018 00:00:00 GMT+0200'), end: new Date('Feb 18, 2018 23:59:59 GMT+0200'), bonus: 20, intersects: false },
{ start: new Date('Feb 19, 2018 00:00:00 GMT+0200'), end: new Date('Feb 29, 2018 23:59:59 GMT+0200'), bonus: 15, intersects: false },
{ start: new Date('Feb 26, 2018 00:00:00 GMT+0200'), end: new Date('Mar 4, 2018 23:59:59 GMT+0200'), bonus: 10, intersects: false },
{ start: new Date('Mar 5, 2018 00:00:00 GMT+0200'), end: new Date('Mar 11, 2018 23:59:59 GMT+0200'), bonus: 5, intersects: false }
];
bonuses
.map(function (a, index) {
if (a.start.getTime() < min) {
min = a.start.getTime();
}
if (a.end.getTime() < min) {
min = a.end.getTime();
}
if (a.start.getTime() > max) {
max = a.start.getTime();
}
if (a.end.getTime() > max) {
max = a.end.getTime();
}
for (var i = index + 1; i < bonuses.length; i++) {
var b = bonuses[i];
if (b.start.getTime() < a.start.getTime() && b.end.getTime() > a.start.getTime() ||
a.start.getTime() < b.start.getTime() && a.end.getTime() > b.start.getTime()) {
bonuses[i].intersects = true;
a.intersects = true;
}
}
return a;
});
var tileSize = 20;
var canvas = document.body.appendChild(document.createElement("canvas"));
canvas.width = 800;
canvas.height = bonuses.length * tileSize;
var ctx = canvas.getContext("2d");
bonuses.forEach(function (bonus, index) {
var start = (bonus.start.getTime() - min) / (max - min);
var end = (bonus.end.getTime() - min) / (max - min);
ctx.fillStyle = bonus.intersects ? "red" : "green";
ctx.fillRect(canvas.width * start, tileSize * index, canvas.width * (end - start), tileSize);
ctx.strokeRect(canvas.width * start, tileSize * index, canvas.width * (end - start), tileSize);
});
&#13;
答案 1 :(得分:0)
var dates = [];
bonuses.forEach(item => {
dates.push(item.start);
dates.push(item.end);
});
dates.sort();
var ranges = [];
for(var i=1;i<dates.length;i++){
ranges.push({
start: dates[i-1],
end: dates[i]
});
}
console.log(ranges);