多个三元运算符

发布于 2024-12-09 13:45:02 字数 282 浏览 1 评论 0原文

我需要一些关于三元运算符的语法帮助,这将帮助我将正确的标记图标放在我的好地图上。我有三个区域 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

陌路终见情 2024-12-16 13:45:02

语法是:

var icon = (area == 1) ? icon1 : (area == 2) ? icon2 : icon0;

但这开始变得复杂。您最好只创建一个函数来完成这项工作:

var icon = getIcon(area);

function getIcon(area) {
  if (area == 1) { 
    return icon1; 
  } else if (area == 2) { 
    return icon2; 
  }

  return icon0;
}

The syntax would be:

var icon = (area == 1) ? icon1 : (area == 2) ? icon2 : icon0;

But this is starting to get complicated. You may well be better off just creating a function to do this work instead:

var icon = getIcon(area);

function getIcon(area) {
  if (area == 1) { 
    return icon1; 
  } else if (area == 2) { 
    return icon2; 
  }

  return icon0;
}
笑咖 2024-12-16 13:45:02

对于任何对多重三元语法感到困惑的人(就像我一样),它是这样的:

var yourVar = condition1 ? someValue
            : condition2 ? anotherValue
            : defaultValue;

您可以添加任意数量的条件。

您可以进一步阅读 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:

var yourVar = condition1 ? someValue
            : condition2 ? anotherValue
            : defaultValue;

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

ま昔日黯然 2024-12-16 13:45:02

怎么样:

var icon = [ icon0, icon1, icon2 ][area];

How about:

var icon = [ icon0, icon1, icon2 ][area];
一城柳絮吹成雪 2024-12-16 13:45:02

对象字面量怎么样?

icons = {
    0: icon0,
    1: icon1,
    2: icon2
}

icon = icons[area];

How about an object literal.

icons = {
    0: icon0,
    1: icon1,
    2: icon2
}

icon = icons[area];
來不及說愛妳 2024-12-16 13:45:02

非常简单的方法

如果你的对象是这样的:

var obj = {
  x: true,
  y: {
    xy: 'some value'
  }
}

var result = obj ? obj.y ? obj.y.xy ? obj.y.xy : 'N/A' : 'N/A' : 'N/A'

console.log(result) // "some value"

Very simple way

If your object is like this:

var obj = {
  x: true,
  y: {
    xy: 'some value'
  }
}

var result = obj ? obj.y ? obj.y.xy ? obj.y.xy : 'N/A' : 'N/A' : 'N/A'

console.log(result) // "some value"

作死小能手 2024-12-16 13:45:02
var icon = (area == 0) ? icon0 : (area == 1) ? icon1 : icon2;
var icon = (area == 0) ? icon0 : (area == 1) ? icon1 : icon2;
十年九夏 2024-12-16 13:45:02

一系列问号运算符 ? 可以返回一个取决于多个条件的值。

例如:

let age = prompt('age?', 18);

let message = (age < 3) ? 'Hi, baby!' :
  (age < 18) ? 'Hello!' :
  (age < 100) ? 'Greetings!' :
  'What an unusual age!';

alert( message ); 

A sequence of question mark operators ? can return a value that depends on more than one condition.

For instance:

let age = prompt('age?', 18);

let message = (age < 3) ? 'Hi, baby!' :
  (age < 18) ? 'Hello!' :
  (age < 100) ? 'Greetings!' :
  'What an unusual age!';

alert( message ); 
执笏见 2024-12-16 13:45:02
condition1 ? (condition2 && condition2*Passed*) || condition2*NotPassed* : condition1NotPassed

现在,如果您的第一个条件通过,那么将考虑第二阶段,如果没有,则 else 部分(在我们的三元组中 : 之后的代码将运行;现在我们认为第一个条件已通过,因此我们处于第二阶段,此代码检查是否在此阶段条件 2 负责并且如果通过,则括号部分的输出将为true,否则括号输出将被收取费用在这种情况下,运算符第二个操作数将被视为 if 语句的 else 部分

condition1 ? (condition2 && condition2*Passed*) || condition2*NotPassed* : condition1NotPassed

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

三生池水覆流年 2024-12-16 13:45:02

使用条件直到默认值的多个三元运算符。

    For eg:- condition1 ? Pass : condition2 ? Pass : condition3 : Pass : Default

    <iframe
    [id]="iFrameId"
    [name]="iFrameId"
    [src]="
      autoPlayEnabled && secondEnabled
        ? (data.iFrameUrl + '?autoplay=1&second=' + secondDuration | safe)
        : autoPlayEnabled && !secondEnabled
        ? (data.iFrameUrl + '?autoplay=1' | safe)
        : !autoPlayEnabled && secondEnabled
        ? (data.iFrameUrl + '?autoplay=1&second=' + secondDuration | safe)
        : (data.iFrameUrl | safe)
    "
  ></iframe>

Multiple ternary operators using condition till default value.

    For eg:- condition1 ? Pass : condition2 ? Pass : condition3 : Pass : Default

    <iframe
    [id]="iFrameId"
    [name]="iFrameId"
    [src]="
      autoPlayEnabled && secondEnabled
        ? (data.iFrameUrl + '?autoplay=1&second=' + secondDuration | safe)
        : autoPlayEnabled && !secondEnabled
        ? (data.iFrameUrl + '?autoplay=1' | safe)
        : !autoPlayEnabled && secondEnabled
        ? (data.iFrameUrl + '?autoplay=1&second=' + secondDuration | safe)
        : (data.iFrameUrl | safe)
    "
  ></iframe>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文