# 如何优化if多层判断

• 4
``````aa(item) {
let color;
let eq1 = "";
let eq2 = "";

for (let i = 0; i < this.option.columnTow.length; i++) {
let element = this.option.columnTow[i];
console.log(parseInt(eq1) < parseInt(item.value))
if (eq1 != "" && eq2 != "" && parseFloat(eq1) < parseFloat(item.value) && parseFloat(item.value) < parseFloat(eq2)) {
color = element.prop;
break;
}
else if (eq1 != "" && eq2 != "" && parseFloat(eq2) < parseFloat(eq1)) {
if (element.equation == "大于" && parseFloat(item.value) > parseFloat(element.value)) {
color = element.prop;
break;
}
else if (element.equation == "小于" && parseFloat(item.value) < parseFloat(element.value)) {
color = element.prop;
break;
}
}
if (element.equation == "大于" && eq2 == "" && parseFloat(item.value) > parseFloat(element.value)) {
color = element.prop;
break;
}
if (element.equation == "小于" && eq1 == "" && parseFloat(item.value) < parseFloat(element.value)) {
color = element.prop;
break;
}
if (element.equation == "等于" && parseFloat(item.value) == parseFloat(element.value)) {
color = element.prop;
break;
}
}
return color;
}``````

5 个回答
``````let color;
let eq1 = "";
let eq2 = "";

for (let i = 0; i < this.option.columnTow.length; i++) {
let element = this.option.columnTow[i];
let eq1Num = parseFloat(eq1);
let eq2Num = parseFloat(eq2);
let itemValNum = parseFloat(item.value);
let eleValNum = parseFloat(element.value);
if (
(eq1 && eq2 && eq1Num < itemValNum && itemValNum < eq2Num) ||
(eq1 &&
eq2 &&
eq2Num < eq1Num &&
((element.equation == "大于" && itemValNum > eleValNum) ||
(element.equation == "小于" && itemValNum < eleValNum))) ||
(element.equation == "大于" && eq2 && itemValNum > eleValNum) ||
(element.equation == "小于" && eq1 && itemValNum < eleValNum) ||
(element.equation == "等于" && itemValNum == eleValNum)
) {
color = element.prop;
break;
}
}
return color;``````