javascript if-else 语句的简写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18269286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:16:37  来源:igfitidea点击:

Shorthand for if-else statement

javascriptif-statement

提问by Meek

I have some code with a lot of if/else statements similar to this:

我有一些代码,其中包含很多与此类似的 if/else 语句:

var name = "true";

if (name == "true") {
    var hasName = 'Y';
} else if (name == "false") {
    var hasName = 'N';
};

But is there a way to make these statements shorter? Something like ? "true" : "false"...

但是有没有办法让这些陈述更短?像? "true" : "false"...

回答by Tushar Gupta - curioustushar

Using the ternary :?operator[spec].

使用三元:?运算符[spec]

var hasName = (name === 'true') ? 'Y' :'N';

The ternary operator lets us write shorthand if..elsestatements exactly like you want.

三元运算符让我们可以if..else完全按照您的意愿编写速记语句。

It looks like:

看起来像:

(name === 'true')- our condition

(name === 'true')- 我们的条件

?- the ternary operator itself

?- 三元运算符本身

'Y'- the result if the condition evaluates to true

'Y'- 条件评估为真时的结果

'N'- the result if the condition evaluates to false

'N'- 如果条件评估为假的结果

So in short (question)?(result if true):(result is false), as you can see - it returns the value of the expression so we can simply assign it to a variable just like in the example above.

简而言之(question)?(result if true):(result is false),正如您所看到的 - 它返回表达式的值,因此我们可以像上面的示例一样简单地将其分配给一个变量。

回答by Benjamin Gruenbaum

You can use an object as a map:

您可以将对象用作地图:

  var hasName = ({
        "true"  : "Y",
        "false" : "N"
  })[name];

This also scales nicely for many options

这也适用于许多选项

  var hasName = ({
        "true"          : "Y",
        "false"         : "N",
        "fileNotFound"  : "O"
  })[name];

(Bonus point for people getting the reference)

(获得参考的人的奖励积分)

Note: you should use actual booleans instead of the string value "true" for your variables indicating truth values.

注意:对于表示真值的变量,您应该使用实际的布尔值而不是字符串值“true”。

回答by Eswara Reddy

Try this

试试这个

hasName = name ? 'Y' : 'N';

回答by Gautam3164

Try like

试试像

var hasName = 'N';
if (name == "true") {
    hasName = 'Y';
}

Or even try with ternary operatorlike

或者甚至尝试ternary operator

var hasName = (name == "true") ? "Y" : "N" ;

Even simply you can try like

即使只是你可以尝试像

var hasName = (name) ? "Y" : "N" ;

Since name has either Yesor Nobut iam not sure with it.

由于名称具有YesNo但我不确定它。

回答by Abijeet Patro

Most answers here will work fine if you have just twoconditions in your if-else. For more which is I guess what you want, you'll be using arrays. Every names corresponding element in namesarray you'll have an element in the hasNamesarray with the exact same index. Then it's a matter of these four lines.

如果您two的 if-else 中只有条件,这里的大多数答案都可以正常工作。对于更多我猜你想要什么,你将使用数组。names数组中的每个名称对应的元素您将在数组中拥有一个hasNames具有完全相同索引的元素。那么就是这四行的问题了。

names = "true";
var names = ["true","false","1","2"];
var hasNames = ["Y","N","true","false"];
var intIndex = names.indexOf(name);
hasName = hasNames[intIndex ];

This method could also be implemented using Objects and properties as illustrated by Benjamin.

此方法也可以使用对象和属性来实现,如Benjamin 所示