'How to check the JavaScript array objects are overlapping
I have an array of objects containing start and end range.
var ranges = [{
start: 1,
end: 5
}]
I want to push an object like this without overlapping with the previous start and end range
{
start: 6,
end: 10
}
How to check new object overlapping with the existing objects
Edit:
{
start: 50,
end: 100
},
{
start: 11,
end: 49
}
Solution 1:[1]
Got the answer thanks everyone for your response.
let isValid = timeRanges.every(r => (r.begin != selected.begin && r.begin < selected.begin && selected.begin > r.end) && (r.end != selected.end && r.end < selected.begin && selected.end > r.end));
Solution 2:[2]
You can try this :
const ranges = [{
start: 1,
end: 5
}];
const rangeObj = {
start: 6,
end: 10
};
function checkRangeOverlap(obj) {
let res = ''
ranges.forEach((obj) => {
res = (obj.end > rangeObj.start) ? 'Overlaping': 'No Overlap';
});
return res;
}
console.log(checkRangeOverlap(rangeObj));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rajesh Donepudi |
| Solution 2 | Rohìt JÃndal |
