0

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>
  }
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830

1 Answers1

0

Just check in forEach-loop of array b that range doesn't contains in array a and push push it to 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)
})
  • Tried your code and received the error `TypeError: a.contains is not a function` – Nyxynyx Jun 26 '20 at 14:17
  • @Nyxynyx, I'm so sorry, here should be `includes`. Check edited code, please – Nikita Iskorkin Jun 27 '20 at 06:06
  • Thank you, however `c` obtained from your solution is still not the desired result. Just edited the original question to compare `c` calculated by your code and the desired `c`. – Nyxynyx Jun 27 '20 at 06:39