簡體   English   中英

計算在一個數組中找到但在另一個數組中沒有的 Moment.js 范圍數組

[英]Calculate an array of Moment.js Ranges found in one array but not in another

給定包含力矩范圍的 2 arrays ab

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中的矩范圍?

對於涉及ab的示例,我們希望得到結果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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM