简体   繁体   English

编写if else语句的更好方法

[英]better way to write if else statement

First time writing Javascript. 第一次编写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. 尝试过地图,但我无法正常工作。

There isn't really a shorter way to write an if statement in that way (which I will assume is what you're asking). 确实没有一种更短的方法来编写if语句(我将假设这是您要的内容)。 However, there could be a few different ways to write this depending on how many things you want to check. 但是,根据要检查的内容,可能有几种不同的编写方法。

Use a Switch statement 使用Switch语句

There is a cleaner way when dealing with multiple cases that letter could be. 处理多个letter可能存在的情况时,有一种更简洁的方法。

This would be a switch statement and it would look like this: 这将是一个switch语句,如下所示:

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;
}

This reads a little better than an if else statement in some cases. 在某些情况下,这比if else语句更好。 The default keyword here acts as your else clause in an if else statement. 此处的default关键字在if else语句中充当else子句。 The case acts as your different if statements if you will. 如果您愿意,该case充当您不同的if语句。

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" ). 本质上,上面的switch语句将遍历它定义的每种情况,直到找到与letter匹配的情况(例如"5544" )为止。 If none matches, it hits the default case. 如果没有匹配项,它将达到default大小写。 The break keyword at the end of each case stops things from falling through to the next defined case once a match is found. 一旦找到匹配项,每种case结尾处的break关键字case阻止事情陷入下一个定义的case

This method could get cumbersome with more than 6 or 7 cases. 超过6或7个案例,此方法可能很麻烦。

Create an object and look up the value 创建一个对象并查找值

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. 如果要检查的值很多,这可能是获取值的简单方法。

Other thoughts 其他想法

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语句称为三元语句,你可以在这里找到: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator 。 ..但是,我不建议嵌套它们,因为它变得非常复杂且不易读。

Conclusion 结论

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. 因此,要重申您的问题的答案: 不,实际上没有一种编写带有许多值的if else语句的较短方法。 You can use a switch statement to make it cleaner. 您可以使用switch语句使它更整洁。 Use the object lookup method if you have many values you would like to check. 如果您有许多要检查的值,请使用对象查找方法。

JavaScript has object (map) literals. JavaScript具有对象(地图)文字。 Use them for terse code. 将它们用于简洁的代码。 In your final application you'll get the data for the map from someplace else and not code it directly into your website, but if you did, it would look like this: 在最终应用程序中,您将从其他地方获取地图数据,而不是直接将其编码到您的网站中,但是如果这样做,它将看起来像这样:

document.getElementById( "demo" ).innerHTML = {
    "5544" : "Abar, Marlon 1,800",
    "5445" : "Pia, Darla 1,800",
    ...
}[ document.getElementById( "myInput" ).value ];

you can use switch for a long if - else -if ladder: 您可以长时间使用switch-if-else -if梯形图:

switch(expression) {
case n:
    code block
    break;
case n:
    code block
    break;
default:
    default code block
}

This is how it works: 它是这样工作的:
1)The switch expression is evaluated once. 1)开关表达式被求值一次。
2)The value of the expression is compared with the values of each case. 2)将表达式的值与每种情况的值进行比较。
3)If there is a match, the associated block of code is executed. 3)如果匹配,则执行相关的代码块。

if you need basic tutorials in java script then you should try w3 schools. 如果您需要Java脚本中的基本教程,则应该尝试W3学校。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM