[英]Calculate an array of Moment.js Ranges found in one array but not in another
给定包含力矩范围的 2 arrays a
和b
,
const Moment = require('moment');
const MomentRange = require('moment-range');
const moment = MomentRange.extendMoment(Moment);
let a = [
moment.range(moment('2020-01-01 09:00'), moment('2020-01-01 11:00')),
moment.range(moment('2020-01-01 14:00'), moment('2020-01-01 18:00')),
moment.range(moment('2020-01-01 20:00'), moment('2020-01-01 21:00')),
moment.range(moment('2020-01-01 22:00'), moment('2020-01-01 23:00'))
];
let b = [
moment.range(moment('2020-01-01 11:30'), moment('2020-01-01 13:00')),
moment.range(moment('2020-01-01 17:00'), moment('2020-01-01 20:30')),
moment.range(moment('2020-01-01 21:45'), moment('2020-01-01 23:15'))
];
我们如何计算一个数组c
包含在b
中找到但不在a
中的矩范围?
对于涉及a
和b
的示例,我们希望得到结果c
,由下式给出
let c = [
moment.range(moment('2020-01-01 11:30'), moment('2020-01-01 13:00')),
moment.range(moment('2020-01-01 18:00'), moment('2020-01-01 20:00')),
moment.range(moment('2020-01-01 21:45'), moment('2020-01-01 22:00')),
moment.range(moment('2020-01-01 23:00'), moment('2020-01-01 23:15'))
];
使用节点 v14.2.0, moment
2.27.0 和moment-range
4.0.2。
@Nikita-Iskorkin 运行你的代码
let c = [];
b.forEach(range => {
if (!a.includes(range))
c.push(range)
})
给出c
的以下结果,它不排除/减去a
中找到的时间范围:
[
t {
start: Moment<2020-01-01T11:30:00-05:00>,
end: Moment<2020-01-01T13:00:00-05:00>
},
t {
start: Moment<2020-01-01T17:00:00-05:00>,
end: Moment<2020-01-01T20:30:00-05:00>
},
t {
start: Moment<2020-01-01T21:45:00-05:00>,
end: Moment<2020-01-01T23:15:00-05:00>
}
]
期望的结果c
应该是:
[
t {
start: Moment<2020-01-01T11:30:00-05:00>,
end: Moment<2020-01-01T13:00:00-05:00>
},
t {
start: Moment<2020-01-01T18:00:00-05:00>,
end: Moment<2020-01-01T20:00:00-05:00>
},
t {
start: Moment<2020-01-01T21:45:00-05:00>,
end: Moment<2020-01-01T22:00:00-05:00>
},
t {
start: Moment<2020-01-01T23:00:00-05:00>,
end: Moment<2020-01-01T23:15:00-05:00>
}
只需检查数组b
的 forEach 循环,该范围不包含在数组a
中,然后将其推送到c
const Moment = require('moment');
const MomentRange = require('moment-range');
const moment = MomentRange.extendMoment(Moment);
let a = [
moment.range(moment('2020-01-01 09:00'), moment('2020-01-01 11:00')),
moment.range(moment('2020-01-01 14:00'), moment('2020-01-01 18:00')),
moment.range(moment('2020-01-01 20:00'), moment('2020-01-01 21:00')),
moment.range(moment('2020-01-01 22:00'), moment('2020-01-01 23:00'))
];
let b = [
moment.range(moment('2020-01-01 11:30'), moment('2020-01-01 13:00')),
moment.range(moment('2020-01-01 17:00'), moment('2020-01-01 20:30')),
moment.range(moment('2020-01-01 21:45'), moment('2020-01-01 23:15'))
];
let c = [];
b.forEach(range => {
if (!a.includes(range))
c.push(range)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.