两个范围是否有重合部分
function isItCoincident(one, two) {
if (
(one.begin >= two.begin && one.begin <= two.end) ||
(one.end >= two.begin && one.end <= two.end) ||
(one.begin >= two.begin && one.end <= two.end) ||
(one.begin <= two.begin && one.end >= two.end)
) {
return true;
} else {
return false;
}
}
一个数据范围数组,去重整合
function aggregateRange(data = []) {
if (data.length <= 1) return data;
let range = data;
range.sort((a, b) => {
return a.begin - b.begin;
});
const result = [];
for (let i = 0; i < range.length;) {
let o = range[i];
let value = o;
let repeat = [i];
for (let j = i + 1; j < range.length; j++) {
let c = range[j];
let coincide = isItCoincident(value, c);
let maxBegin = value.begin >= c.begin ? c.begin : value.begin;
let maxEnd = value.end >= c.end ? value.end : c.end;
if (coincide) {
value = {
begin: maxBegin,
end: maxEnd
};
repeat.push(j);
}
}
range = range.filter((_, index) => !repeat.includes(index));
result.push(value);
}
return result;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39