• <tfoot id='WICCr'></tfoot>
    • <bdo id='WICCr'></bdo><ul id='WICCr'></ul>

  • <i id='WICCr'><tr id='WICCr'><dt id='WICCr'><q id='WICCr'><span id='WICCr'><b id='WICCr'><form id='WICCr'><ins id='WICCr'></ins><ul id='WICCr'></ul><sub id='WICCr'></sub></form><legend id='WICCr'></legend><bdo id='WICCr'><pre id='WICCr'><center id='WICCr'></center></pre></bdo></b><th id='WICCr'></th></span></q></dt></tr></i><div id='WICCr'><tfoot id='WICCr'></tfoot><dl id='WICCr'><fieldset id='WICCr'></fieldset></dl></div>

      <small id='WICCr'></small><noframes id='WICCr'>

      <legend id='WICCr'><style id='WICCr'><dir id='WICCr'><q id='WICCr'></q></dir></style></legend>

        具有多个条件的JS三元函数?

        时间:2023-10-01

            <legend id='5sDIh'><style id='5sDIh'><dir id='5sDIh'><q id='5sDIh'></q></dir></style></legend>

            <small id='5sDIh'></small><noframes id='5sDIh'>

              <tbody id='5sDIh'></tbody>
              <tfoot id='5sDIh'></tfoot>
              <i id='5sDIh'><tr id='5sDIh'><dt id='5sDIh'><q id='5sDIh'><span id='5sDIh'><b id='5sDIh'><form id='5sDIh'><ins id='5sDIh'></ins><ul id='5sDIh'></ul><sub id='5sDIh'></sub></form><legend id='5sDIh'></legend><bdo id='5sDIh'><pre id='5sDIh'><center id='5sDIh'></center></pre></bdo></b><th id='5sDIh'></th></span></q></dt></tr></i><div id='5sDIh'><tfoot id='5sDIh'></tfoot><dl id='5sDIh'><fieldset id='5sDIh'></fieldset></dl></div>
                <bdo id='5sDIh'></bdo><ul id='5sDIh'></ul>
                1. 本文介绍了具有多个条件的JS三元函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在 JavaScript 中使用三元运算符来根据用户输入修改对象的值.我有以下代码,它应该运行:

                  I have been using a ternary operator in JavaScript to modify the value of an object based on user input. I have the following code, which runs as it should:

                  var inputOneAns = inputOne == "Yes" ? "517" : "518";
                  

                  如您所见,我将一个数字字符串值分配给 inputOneAns,无论用户输入的是是"还是否".但是,可能存在用户没有选择值的情况(因为它不是必需的).如果此输入留空,我想将一个空字符串"分配给 inputOneAns.有没有办法或我将一个三元运算符嵌入另一个三元运算符中?为了帮助澄清,这是我想用我的三元函数但使用 if else 语句来完成的相同函数?

                  As you can see, I am assigning a numeric string value to inputOneAnswhether a user has inputed "Yes" or "No". However, there may be a case that a user has not selected a value (as it is not required). If this input was left blank, I would like to assign an empty string "" to inputOneAns. Is there a wayf or me to embed an ternary operator inside of another ternary operator? To help clarify, here is the same function that I want to accompolish with my ternary function but with if else statements?

                  if (inputOne == "Yes"){
                      var inputOneAns = "517"
                  }else if (inputOne == "No"{
                      var inputOneAns = "518"
                  }else{
                      var inputOneAns = ""
                  }
                  

                  是否可以在一个三元函数中包含多个表达式?有没有更好的方法来完成我正在寻找的东西?提前感谢您的提示.

                  Is it possible to include multiple expressions into a ternary function? Is there a better way to accomplish what I am looking for? Thanks for the tips in advance.

                  推荐答案

                  是的,你可以疯狂地嵌套三元组.我觉得这个版本相当易读:

                  Yes you can go wild nesting ternaries. I find this version to be fairly readable:

                  var foo = (
                    bar === 'a' ? 1 : // if 
                    bar === 'b' ? 2 : // else if 
                    bar === 'c' ? 3 : // else if
                    null // else 
                  );
                  

                  但这并不是一个广泛认同的观点,在团队中工作时您可能应该坚持使用 if/elseswitch.

                  but that's not a widely shared opinion, and you should probably stick to if/else or switch when working on a team.

                  这篇关于具有多个条件的JS三元函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:仅使用 for 循环和条件语句的 Javascript 打印方块 下一篇:使用'&amp;&amp;'有什么区别和'||'通过三元运算符('

                  相关文章

                2. <i id='Cw06N'><tr id='Cw06N'><dt id='Cw06N'><q id='Cw06N'><span id='Cw06N'><b id='Cw06N'><form id='Cw06N'><ins id='Cw06N'></ins><ul id='Cw06N'></ul><sub id='Cw06N'></sub></form><legend id='Cw06N'></legend><bdo id='Cw06N'><pre id='Cw06N'><center id='Cw06N'></center></pre></bdo></b><th id='Cw06N'></th></span></q></dt></tr></i><div id='Cw06N'><tfoot id='Cw06N'></tfoot><dl id='Cw06N'><fieldset id='Cw06N'></fieldset></dl></div>
                  1. <small id='Cw06N'></small><noframes id='Cw06N'>

                    <tfoot id='Cw06N'></tfoot>

                    <legend id='Cw06N'><style id='Cw06N'><dir id='Cw06N'><q id='Cw06N'></q></dir></style></legend>

                      <bdo id='Cw06N'></bdo><ul id='Cw06N'></ul>