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

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

      <tfoot id='hyvne'></tfoot>
    1. <small id='hyvne'></small><noframes id='hyvne'>

    2. <legend id='hyvne'><style id='hyvne'><dir id='hyvne'><q id='hyvne'></q></dir></style></legend>
      1. 使用字符串“includes()";在切换 Javascript 的情况下

        时间:2023-09-07

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

              <bdo id='YKhq2'></bdo><ul id='YKhq2'></ul>
                <tbody id='YKhq2'></tbody>
              1. <small id='YKhq2'></small><noframes id='YKhq2'>

                <legend id='YKhq2'><style id='YKhq2'><dir id='YKhq2'><q id='YKhq2'></q></dir></style></legend>
                <tfoot id='YKhq2'></tfoot>
                • 本文介绍了使用字符串“includes()";在切换 Javascript 的情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在Javascript中,有没有办法实现类似的东西?

                  In Javascript, is there a way to achieve something similar to this ?

                  const databaseObjectID = "someId"; // like "product/217637"
                  
                  switch(databaseObjectID) {
                      case includes('product'): actionOnProduct(databaseObjectID); break;
                      case includes('user'): actionOnUser(databaseObjectID); break;
                      // .. a long list of different object types
                  }
                  

                  这更像是一个好奇的问题,以了解 switch/case 的可能性,因为在这种特殊情况下,我使用 const type = databaseObjectID.split('/')[0]; 解决了我的问题; 并在 type

                  This is more a curiosity question to understand the possibilities of switch / case, as in this particular case I have solved my problem using const type = databaseObjectID.split('/')[0]; and apply the switch case on type

                  推荐答案

                  您的使用将被视为滥用案例.

                  You usage would be considered an abuse of case.

                  而只是使用 ifs

                       if (databaseObjectId.includes('product')) actionOnProduct(databaseObjectID); 
                  else if (databaseObjectId.includes('user'))    actionOnUser(databaseObjectID); 
                  // .. a long list of different object types
                  

                  如果 ObjectId 包含产品或用户周围的静态内容,您可以将其移除并使用用户或产品作为键:

                  If the ObjectId contains static content around the product or user, you can remove it and use the user or product as a key:

                  var actions = {
                    "product":actionOnProduct,
                    "user"   :actionOnUser
                  }
                  
                  actions[databaseObjectId.replace(/..../,"")](databaseObjectId);
                  

                  这篇关于使用字符串“includes()";在切换 Javascript 的情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:是否可以在 switch 语句中使用 .contains() ? 下一篇:切换案例到/之间

                  相关文章

                    <bdo id='Jmde6'></bdo><ul id='Jmde6'></ul>
                • <small id='Jmde6'></small><noframes id='Jmde6'>

                  <tfoot id='Jmde6'></tfoot>

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