[英]How can I make a large if/else statement tighter?
I have something like the following: 我有类似以下内容:
if (value === section1) {
runChecks(checkObject[1].value1, checkObject[1].value2, leftAlign);
} else if (value === section2) {
runChecks(checkObject[2].value1, checkObject[2].value2, rightAlign);
} else if (value === section3) {
runChecks(checkObject[3].value1, checkObject[3].value2, leftAlign);
} else if (value === section4) {
runChecks(checkObject[4].value1, checkObject[4].value2, rightAlign);
} else if (value === section5) {
runChecks(checkObject[5].value1, checkObject[5].value2, leftAlign, true);
} else if (value === section6) {
runChecks(checkObject[6].value1, checkObject[6].value2, rightAlign);
} ...
It runs longer than this as there's a large number of pre-defined values. 由于存在大量的预定义值,因此它的运行时间比此时间长。
Example of checkObject: checkObject的示例:
var checkObject = [{
value1: '19.1%',
value2: '19.1%',
}, {
value1: '19.1%',
value2: '19.1%',
}, {
value1: '19.1%',
value2: '19.1%',
}, ...
I want to break it down and make it more efficient but given there's variations on the data being passed to runChecks() I'm not sure how to manage it. 我想将其分解并提高效率,但是鉴于传递给runChecks()的数据存在差异,因此我不确定如何进行管理。
Use switch! 使用开关!
switch(value){
case section1:
runChecks(checkObject[1].value1, checkObject[1].value2, leftAlign);
break
case section2:
runChecks(checkObject[2].value1, checkObject[2].value2, rightAlign);
break
...
}
It's not really any shorter than using if / else if / else
but it looks cleaner. 它实际上并不比使用if / else if / else
更短,但看起来更干净。
Actually, you can just use a for loop. 实际上,您可以只使用for循环。
Create an array object containing all the sections. 创建一个包含所有节的数组对象。 (I will provide the real javascript later. But here is some half-pseudocode). (稍后我将提供真正的javascript。但这是一些半伪代码)。
var counter = 1;
var N = something;
var sectionArray = {section1, section2, section3, ..., sectionN};
for(;counter<N; counter++){
if(value === sectionArray[counter]){
runChecks(checkObject[counter].value1, checkObject[counter].value2, rightAlign);
break;
}
}
One way would be to use loop for instance. 一种方法是例如使用循环。
let sections = ['section1', 'section2', 'section3', 'section4'];
for (let i = 0; i < sections.length; i++) {
if (value === sections[i]) {
runChecks(checkObject[1].value1, checkObject[1].value2, leftAlign);
}
}
You could use some
with a short circuit if the value is found. 你可以使用some
,如果该值发现短路。
[section1, section2, section3, section4, section5, section6].some((v, i) => {
if (v === value) {
runChecks(
checkObject[i + 1].value1,
checkObject[i + 1].value2,
i % 2 ? rightAlign : leftAlign
i === 5 || undefined
);
return true;
}
});
Couldn't you iterate over the array? 您不能遍历数组吗? You might need to add all information that you require in the runChecks function though (I added mybool, since I wasn't sure where this true is coming from). 但是,您可能需要在runChecks函数中添加所需的所有信息(我添加了mybool,因为我不确定这是从哪里来的)。
checkObject.forEach(function(value, index) {
let alignment = index % 2 == 0 ? leftAlign : rightAlign;
runChecks(value.value1, value.value2, alignment, value.mybool);
});
There are too many repeating parts in your code. 您的代码中有太多重复的部分。 I would do it this way. 我会这样做。
var scs = [null, section1, section2, section3, section4, section5, section6];
var si = scs.indexOf(value);
var align = ((si&1)==1) ? leftAlign : rightAlign;
runChecks(checkObject[si].value1, checkObject[si].value2, align, si==5);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.