Home > Mobile >  Using switch case in javascript
Using switch case in javascript

Time:11-22

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. I am trying to put those flags in a switch case and call individual function when each flag is true.Something like this,

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. Can someone point me in the right direction?

CodePudding user response:

Don't convert them to strings and in switch condition add just 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;
            }
        }

CodePudding user response:

switch is not the right construct to use in this case. Simply use if/else here.

CodePudding user response:

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. (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:

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 (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");
    }
}

CodePudding user response:

A switch statement can only interrogate one variable. In your case the correct solution is an if statement for each member variable. Replace the switch statement with this snippet:

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.

  • Related