编写if else语句的更好方法 [英] better way to write if else statement
问题描述
第一次编写Javascript.我只是想知道是否有一种较短的书写方式:
First time writing Javascript. I just would like know if there is a shorter way of writing this:
<p id="demo"></p>
<script>
function myFunction() {
var letter = document.getElementById("myInput").value;
var text;
if (letter === "5544") {
text = "Abar, Marlon 1,800";
} else if (letter === "5545") {
text = "Pia, Darla 1,800";
} else if (letter === "5546") {
text = "Salazar, Alex 1,500";
//etc...
} else {
text = "Incorrect Account Number";
}
document.getElementById("demo").innerHTML = text;
}
</script>
尝试过地图,但无法正常工作.
Tried map but I couldn't get it to work.
推荐答案
用这种方式编写if
语句实际上并没有更短的方法(我想这就是您要的内容).但是,根据要检查的内容,可能有几种不同的编写方式.
There isn't really a shorter way to write an if
statement in that way (which I will assume is what you're asking). However, there could be a few different ways to write this depending on how many things you want to check.
在处理多个可能属于letter
的案件时,有一种更简洁的方法.
There is a cleaner way when dealing with multiple cases that letter
could be.
这将是switch
语句,看起来像这样:
This would be a switch
statement and it would look like this:
var text;
switch (letter) {
case "5544":
text = "Abar, Marlon 1,800";
break;
case "5545":
text = "Pia, Darla 1,800";
break;
// more cases
default:
text = "Incorrect Account Number";
break;
}
在某些情况下,此语句的读取效果比if else
语句好.此处的default
关键字用作if else
语句中的else
子句.如果愿意,case
会充当您不同的if
语句.
This reads a little better than an if else
statement in some cases. The default
keyword here acts as your else
clause in an if else
statement. The case
acts as your different if
statements if you will.
从本质上讲,上面的switch
语句将遍历它定义的每种情况,直到找到与letter
匹配的情况(例如"5544"
).如果没有匹配项,则按default
大小写.一旦找到匹配项,每个case
末尾的break
关键字就可以阻止内容进入下一个定义的case
.
Essentially, the switch
statement above will fall through each of the cases it defines until it finds a case that matches letter
(such as "5544"
). If none matches, it hits the default
case. The break
keyword at the end of each case
stops things from falling through to the next defined case
once a match is found.
这种方法在处理6或7例以上时会很麻烦.
This method could get cumbersome with more than 6 or 7 cases.
现在,获取所需值的更简单方法可以是定义一个对象并根据输入的内容获取值,如下所示:
Now, a shorter way to get the value you want could be to define an object and get the value based on what has been entered like so:
var letter = document.getElementById('selector').value;
var obj = {
'5544': 'Abar, Marlon 1,800'
};
if (letter in obj) {
// do something if found
}
else {
// do something if not found
}
如果要检查的值很多,这可能是获取值的简单方法.
This could be an easy way to get a value if you have many values to check.
作为所有这些的补充说明,有一些简短的if
语句(称为三元语句),您可以在这里找到:
As a side note to all of this, there are short hand if
statements called ternary statements which you can find here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator ... However, I would not recommend nesting these as it becomes very complicated and not very readable.
因此,要重申您的问题的答案:否,实际上并没有一种更短的方法来编写具有许多值的if else
语句.您可以使用switch
语句使它更整洁.如果您有许多要检查的值,请使用对象查找方法.
So, to reiterate the answer to your question: No, there isn't really a shorter way to write an if else
statement with many values. You can use a switch
statement to make it cleaner. Use the object lookup method if you have many values you would like to check.
这篇关于编写if else语句的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!