简体   繁体   English

在 javascript 中使用开关盒

[英]Using switch case in javascript

This is the variable i am having right now这是我现在拥有的变量

[
   {
      "_id":"63773059c3160f782c087e33",
      "nfrid":"637328ebf5c4b2558b064809",
      "nfrname":"azuread",
      "fileName":"package.json",
      "isImport":false,
      "isConst":false,
      "isComponent":false,
      "isNewFile":false,
      "landmark":"\"react\"",
      "isAfter":false,
      "fileContent":"\"@azure/msal-react\": \"^1.4.9\",",
      "filePath":"package.json",
      "isPackage":true,
      "isIndexHtml":false,
      "projecttypeid":"6372366d1b568e00d8af2e44",
      "projecttypetitle":"PWA React",
      "nfrGitIo":[
         {
            "_id":"637328ebf5c4b2558b064809",
            "iconpath":"https://cdnerapidxdevportal.azureedge.net/webdesignerimages/azure-active-directory-aad-icon-488x512-3d71nrtk.png",
            "title":"Azure AD",
            "description":"Azure Active Directory (Azure AD), part of Microsoft Entra, is an enterprise identity service that provides single sign-on, multifactor authentication, and conditional access to guard against 99.9 percent of cybersecurity attacks."
         }
      ]
   },
   {
      "_id":"63773144c3160f782c087e35",
      "nfrid":"637328ebf5c4b2558b064809",
      "nfrname":"azuread",
      "fileName":"index.js",
      "isImport":true,
      "isConst":false,
      "isComponent":false,
      "isNewFile":false,
      "isPackage":false,
      "landmark":null,
      "isAfter":null,
      "fileContent":"import { MsalProvider } from '@azure/msal-react';import { msalConfig } from './authConfig';import {PublicClientApplication } from '@azure/msal-browser';",
      "filePath":"src/index.js",
      "isIndexHtml":false,
      "projecttypeid":"6372366d1b568e00d8af2e44",
      "projecttypetitle":"PWA React",
      "nfrGitIo":[
         {
            "_id":"637328ebf5c4b2558b064809",
            "iconpath":"https://cdnerapidxdevportal.azureedge.net/webdesignerimages/azure-active-directory-aad-icon-488x512-3d71nrtk.png",
            "title":"Azure AD",
            "description":"Azure Active Directory (Azure AD), part of Microsoft Entra, is an enterprise identity service that provides single sign-on, multifactor authentication, and conditional access to guard against 99.9 percent of cybersecurity attacks."
         }
      ]
   },
]

I am having many flags like isImport, isPackage, isIndexHtml like that.我有很多标志,比如 isImport、isPackage、isIndexHtml 之类的。 I am trying to put those flags in a switch case and call individual function when each flag is true.Something like this,我试图将这些标志放在一个开关盒中,并在每个标志为真时调用个人 function。就像这样,

for (let i = 0; i < cosmos.length; i++) {
        console.log(cosmos[0].isPackage);
        switch (cosmos[i]) {
            case `${cosmos[i].isImport  === true}`:
                const statusImport = common.updateImport(cosmos[i]);
                console.log(statusImport);
                break;
            // case `${cosmos[i].isConst === true}`:
            //     console.log("I own a dog");
            //     break;
            case `${cosmos[i].isPackage === true}`:
                const statusPackage = common.updatePackage(cosmos[i]);
                console.log(statusPackage);
                break;
            case `${cosmos[i].isIndexHtml === true}`:
                const statusIndexHtml = common.updateIndexHTML(cosmos[i]);
                console.log(statusIndexHtml);              
                break;
            // case `${cosmos[i].isNewFile === true}`:
            //     const statusNewFile = common.addNewFile(cosmos[i]);
            //     console.log(statusNewFile);
            //     break;
            default:
                console.log("Nothing to add/update");
                break;
            }
        }

But when I run this i am always getting the default console log.但是当我运行它时,我总是得到默认的控制台日志。 I dont know what i am missing我不知道我错过了什么

This is my first switch case implementation.这是我的第一个 switch case 实现。 Can someone point me in the right direction?有人能指出我正确的方向吗?

Don't convert them to strings and in switch condition add just true :不要将它们转换为字符串,在 switch 条件下只添加true

for (let i = 0; i < cosmos.length; i++) {
        console.log(cosmos[0].isPackage);
        switch (true) {
            case cosmos[i].isImport:
                const statusImport = common.updateImport(cosmos[i]);
                console.log(statusImport);
                break;
            case cosmos[i].isPackage:
                const statusPackage = common.updatePackage(cosmos[i]);
                console.log(statusPackage);
                break;
            case cosmos[i].isIndexHtml:
                const statusIndexHtml = common.updateIndexHTML(cosmos[i]);
                console.log(statusIndexHtml);              
                break;
            default:
                console.log("Nothing to add/update");
                break;
            }
        }

switch is not the right construct to use in this case. switch不是在这种情况下使用的正确构造。 Simply use if/else here.在这里简单地使用if/else

Since you're testing several different values from cosmos[i] , not testing a single value against multiple possible matches, switch isn't the right tool here.由于您正在测试cosmos[i]中的几个不同值,而不是针对多个可能的匹配项测试单个值,因此switch不是这里的正确工具。 (You can use it, just like you can use a wrench to bang in a nail, but it's not the right tool.) Instead, use an if / else if / else chain: (你可以使用它,就像你可以用扳手敲钉子一样,但它不是正确的工具。)相反,使用if / else if / else链:

for (let i = 0; i < cosmos.length; i++) {
    if (cosmos[i].isImport) {
        const statusImport = common.updateImport(cosmos[i]);
        console.log(statusImport);
    } else if (cosmos[i].isPackage) {
        const statusPackage = common.updatePackage(cosmos[i]);
        console.log(statusPackage);
    } else if (cosmos[i].isIndexHtml) {
        const statusIndexHtml = common.updateIndexHTML(cosmos[i]);
        console.log(statusIndexHtml);
    } else {
        console.log("Nothing to add/update");
    }
}

Separately, in new code, I'd suggest using a for-of instead of a for when you don't need the index:另外,在新代码中,我建议在不需要索引时使用for-of而不是for

for (const entry of cosmos) {
    if (entry.isImport) {
        const statusImport = common.updateImport(entry);
        console.log(statusImport);
    } else if (entry.isPackage) {
        const statusPackage = common.updatePackage(entry);
        console.log(statusPackage);
    } else if (entry.isIndexHtml) {
        const statusIndexHtml = common.updateIndexHTML(entry);
        console.log(statusIndexHtml);
    } else {
        console.log("Nothing to add/update");
    }
}

A switch statement can only interrogate one variable.一条switch语句只能询问一个变量。 In your case the correct solution is an if statement for each member variable.在您的情况下,正确的解决方案是每个成员变量的if语句。 Replace the switch statement with this snippet:用这个片段替换 switch 语句:

 if (cosmos[i].isImport === true) { const statusImport = common.updateImport(cosmos[i]); console.log(statusImport); } if (cosmos[i].isPackage === true) { const statusPackage = common.updatePackage(cosmos[i]); console.log(statusPackage); } if (cosmos[i].isIndexHtml === true) { const statusIndexHtml = common.updateIndexHTML(cosmos[i]); console.log(statusIndexHtml); }

I note that your data structure does not mutually exclude the isImport isPackage and isIndexHtml - so in principle any combination of them could be true and my proposed code would execute accordingly.我注意到您的数据结构不会相互排除isImport isPackageisIndexHtml - 因此原则上它们的任何组合都可能是正确的,并且我建议的代码将相应地执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM