[英]Calculate an array of Moment.js Ranges found in one array but not in another
Given the 2 arrays a
and b
containing moment ranges ,给定包含力矩范围的 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'))
];
How can we calculate an array c
containing moment ranges that are found in b
but not in a
?我们如何计算一个数组
c
包含在b
中找到但不在a
中的矩范围?
For an example involving a
and b
, we want to get the result c
, given by对于涉及
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'))
];
Using node v14.2.0, moment
2.27.0 and moment-range
4.0.2.使用节点 v14.2.0,
moment
2.27.0 和moment-range
4.0.2。
@Nikita-Iskorkin Running your code @Nikita-Iskorkin 运行你的代码
let c = [];
b.forEach(range => {
if (!a.includes(range))
c.push(range)
})
gives the following result for c
which does not exclude/subtract away the time ranges found in a
:给出
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>
}
]
The desired result c
should be:期望的结果
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>
}
Just check in forEach-loop of array b
that range doesn't contains in array a
and push push it to c
只需检查数组
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.