js 计算所选区间是否存在重合区间算法 重合 区间重合
思路:
1.使用计算公式进行区间的值创建
2.通过数据创建区间
3.循环判断区间是否重叠
参考:
https://www.jianshu.com/p/e20ab76f3372
注意公式:
-两个区间,一个[A,B],一个 [X,Y]
得到有交集的情况:X < B && A < Y。
函数代码:
IntervalValidation (list) { // list 列表数据
let interval = {} // 区间集合
list.forEach((res, i) => {
if (!interval[res.guaranteeVarieties]) interval[res.guaranteeVarieties] = []
if (res.conditionalOperator === '01') { // 小于符号
interval[res.guaranteeVarieties].push({min: 0, max: parseInt(res.conditionalLeft) - 1})
} else if (res.conditionalOperator === '02') { // 小于等于
interval[res.guaranteeVarieties].push({min: 0, max: parseInt(res.conditionalLeft)})
} else if (res.conditionalOperator === '03') { // 大于
interval[res.guaranteeVarieties].push({min: parseInt(res.conditionalLeft) + 1, max: 999999999})
} else if (res.conditionalOperator === '04') { // 大于等于
interval[res.guaranteeVarieties].push({min: parseInt(res.conditionalLeft), max: 999999999})
} else if (res.conditionalOperator === '05') { // 大于&小于
interval[res.guaranteeVarieties].push({min: parseInt(res.conditionalLeft) + 1, max: parseInt(res.conditionalRight) - 1})
} else if (res.conditionalOperator === '06') { // 大于等于&小于
interval[res.guaranteeVarieties].push({min: parseInt(res.conditionalLeft), max: parseInt(res.conditionalRight) - 1})
} else if (res.conditionalOperator === '07') { // 大于&小于等于
interval[res.guaranteeVarieties].push({min: parseInt(res.conditionalLeft) + 1, max: parseInt(res.conditionalRight)})
} else if (res.conditionalOperator === '08') { // 大于等于&小于等于
interval[res.guaranteeVarieties].push({min: parseInt(res.conditionalLeft), max: parseInt(res.conditionalRight)})
}
})
let repetition = false // 区间是否重复
for (let n in interval) { // n 表示对象的属性名
if (interval[n].length > 1) {
for (let j = 0; j < interval[n].length; j++) {
for (let k = 0; k < interval[n].length; k++) {
if (j !== k) { // 排除他本身
if (interval[n][j].max >= interval[n][k].min && interval[n][j].min <= interval[n][k].max) {
this.$message.warning('区间存在重叠请重新选择')
repetition = true
break
}
}
}
if (repetition) {
break
}
}
}
}
if (repetition) {
return true // 重叠
} else {
return false // 不重叠
}
},