多个三元运算符
我需要一些关于三元运算符的语法帮助,这将帮助我将正确的标记图标放在我的好地图上。我有三个区域 0、1 和 2,它们具有独特的图标 0、1 和 2。
我以前只有两个区域,所以这个三元运算符工作得很好;
var icon = (area == 1) ? icon1 : icon0;
现在我需要为区域 2 添加第三个图标 (icon2)。
我尝试了各种方法,但似乎都无法做到正确。
I need a bit of syntax help with a ternary operator which will help me to put the correct marker icons on to my good map. I have three areas 0, 1 and 2 which have unique icons 0, 1 and 2.
I used to have just two areas so this ternary operator worked fine;
var icon = (area == 1) ? icon1 : icon0;
Now I need to add an additional third icon (icon2) for area2.
I've tried various methods but just can't seem to get it right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
语法是:
但这开始变得复杂。您最好只创建一个函数来完成这项工作:
The syntax would be:
But this is starting to get complicated. You may well be better off just creating a function to do this work instead:
对于任何对多重三元语法感到困惑的人(就像我一样),它是这样的:
您可以添加任意数量的条件。
您可以进一步阅读 https://developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
For anyone that's confused about the multiple ternary syntax (like I was), it goes like this:
You can add as many conditions as you like.
You can read up further on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
怎么样:
How about:
对象字面量怎么样?
How about an object literal.
非常简单的方法
如果你的对象是这样的:
Very simple way
If your object is like this:
一系列问号运算符
?
可以返回一个取决于多个条件的值。例如:
A sequence of question mark operators
?
can return a value that depends on more than one condition.For instance:
现在,如果您的第一个条件通过,那么将考虑第二阶段,如果没有,则 else 部分(在我们的三元组中
:
之后的代码将运行;现在我们认为第一个条件已通过,因此我们处于第二阶段,此代码检查是否在此阶段条件 2 负责并且如果通过,则括号部分的输出将为true,否则括号输出将被收取或费用在这种情况下,运算符第二个操作数将被视为 if 语句的 else 部分Now if your first condition is passed then the second phase will be taken into account if not, else section (in our ternary the code after
:
will run; now we consider that the first condition is passed so we are in 2nd phase and this code checks that if in this phase condition 2 is in charge and if it is passed then the output of parentheses section will be true else the parentheses output will be charged with or operator in this case the second operand will be taken as else part of if statement使用条件直到默认值的多个三元运算符。
Multiple ternary operators using condition till default value.