![](/img/trans.png)
[英]How to reduce the complexity of this if else statements in Javascript?
[英]How to reduce if/else complexity?
我有一些if / else语句嵌套,但我想减少它们的开销。
在示例中,我正在评估从哪个下拉列表中单击一个li项,以及该li项是否为第一个(currentIndex === 0)。
编码:
if (parentIndex === 1) {
// But its own index is 0
if (currentIndex === 0) {
parentIndex = 2;
updatedIndexPos = array[1];
mainIndex =list_2.indexOf(updatedIndexPos);
// If the previous line is -1:
if (mainIndex === -1) {
parentIndex = 1;
updatedIndexPos = filterIndexPos;
mainIndex = list_1.indexOf(updatedIndexPos);
}
} else {
mainIndex = list_1.indexOf(mainIndexPos);
}
} else if (parentIndex === 2) {
// But its own index is 0
if (currentIndex === 0) {
parentIndex = 1;
updatedIndexPos = array[0];
mainIndex = list_1.indexOf(updatedIndexPos);
// If the previous line is -1:
if (mainIndex === -1) {
parentIndex = 2;
updatedIndexPos = filterIndexPos;
mainIndex = list_2.indexOf(updatedIndexPos);
}
} else {
mainIndex = list_2.indexOf(mainIndexPos);
}
}
看着它,有很多可重用的代码,而eslint给我带来了7的复杂性。 我尝试将其分解为较小的函数并传递args,但仍无法解决此代码块的复杂性。
尝试提取常见部分,例如(未测试):
if (parentIndex === 1) {
potentialNewParent = 2;
potentialUpdatedIndexPos = array[1];
nullParentIndex = 1;
mainList = list_1;
otherList = list_2;
} else {
// Do the same
}
if (currentIndex === 0) {
parentIndex = potentialNewParent;
updatedIndexPos = potentialUpdatedIndexPos;
mainIndex = mainList.indexOf(updatedIndexPos);
// If the previous line is -1:
if (mainIndex === -1) {
parentIndex = nullParentIndex;
updatedIndexPos = filterIndexPos;
mainIndex = otherList.indexOf(updatedIndexPos);
}
} else {
mainIndex = otherList.indexOf(mainIndexPos);
}
将逻辑建模为状态机通常会很有帮助。 因此,您已经定义了状态和它们之间的过渡。
var action = 'foo';
var actionParams = {...};
var currentState = getState({parentIndex: parentIndex, ...});
if (currentState.canPerform(action)) {
currentState.perform(action, actionParams);
}
假设parentIndex总是1或2,则可以简化很多。 由于我不知道该怎么办,因此我没有测试过,变量名可能不合适:
const mainList = parentIndex === 1 ? list_1 : list_2;
const secondaryList = parentIndex === 1 ? list_2 : list_1;
if (currentIndex === 0) {
parentIndex = parentIndex === 1 ? 2 : 1;
updateIndexPos = array[parentIndex - 1];
mainIndex = secondaryList.indexOf(updatedIndexPos);
if (mainIndex === -1) {
parentIndex = parentIndex === 1 ? 2 : 1;
updatedIndexPos = filterIndexPos;
mainIndex = mainList.indexOf(updatedIndexPos);
}
} else {
mainIndex = mainList.indexOf(mainIndexPos);
}
假设parentIndex只能是1或2,这对您有用吗?
var elements = [2, 1];
if ((parentIndex === 1 || parentIndex === 2) && currentIndex === 0) {
oldParentIndex = parentIndex;
parentIndex = elements[oldParentIndex-1];
updatedIndexPos = array[oldParentIndex%2];
mainIndex = this["list_"+parentIndex].indexOf(updatedIndexPos);
if (mainIndex === -1) {
parentIndex = oldParentIndex;
updatedIndexPos = filterIndexPos;
mainIndex = this["list_"+oldParentIndex].indexOf(updatedIndexPos);
}
} else if ((parentIndex === 1 || parentIndex === 2) && currentIndex !== 0) {
mainIndex = this["list_"+parentIndex].indexOf(updatedIndexPos);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.