Reputation: 377
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
Upvotes: 1
Views: 367
Reputation: 36574
You can use 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)));
Upvotes: 3