简体   繁体   English

检查某个时间范围是否不在其他时间范围内?

[英]Check if a time range is not within other time ranges?

I need to check if the given time range is available or not. 我需要检查给定的时间范围是否可用。

Array with busy hours: 繁忙时间的阵列:

[
  { from: 2, to: 4 },
  { from: 8, to: 10 }
]

The compartment I want to check 我要检查的车厢

{ from: 3, to: 6 }

Expected result: 预期结果:

{ from: 1, to: 2 } // true
{ from: 5, to: 7 } // true
{ from: 3, to: 4 } // false
{ from: 9, to: 10 } // false

You can use some() 您可以使用some()

 const arr = [ { from: 2, to: 4 }, { from: 8, to: 10 } ] function checkTime(arr,obj){ return !arr.some(x => x.from <= obj.from && x.to >= obj.to); } let tests = [ { from: 1, to: 2 }, { from: 5, to: 7 }, { from: 3, to: 4 }, { from: 9, to: 10 } ] tests.forEach(x => console.log(checkTime(arr,x))); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM