Given the 2 arrays a
and b
containing moment ranges,
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
?
For an example involving a
and b
, we want to get the result c
, given by
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.
@Nikita-Iskorkin Running your code
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
:
[
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:
[
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>
}