Looking for a specific value in a switch case code written in JavaScript

Below is the code snippet found in my alert box or any variable. I need to update the values in product to different values such as 'Tremfya', 'Remicade', dynamically. These values are required for a specific product like "linename" and "wheretonavigate". How can I create a JavaScript function to achieve this?

function roadmaplist(product){
    var product = '#~Product~#';
    var PatientRoadMapList;
    switch (product) 
    {
        case 'Tremfya':
        {
            PatientRoadMapList = 
            [
                {
                    "linename" : "Initiate Prior Authorization",
                    "whereToNavigate" : "InsuranceCoverage"
                },
                {
                    "linename" : "Instant Benefits Investigation",
                    "whereToNavigate" : "InsuranceCoverage"
                },
                {
                    "linename" : "Check Eligibility and Enroll Patient into Savings Program",
                    "whereToNavigate" : "SavingsProgram"
                },
                {
                    "linename" : "Enroll Patient into PsO Simple",
                    "whereToNavigate" : "TreatmentSupport"
                },
                {
                    "linename" : "Upload Signed Prescription",
                    "whereToNavigate" : "InsuranceCoverage"
                }
            ];
            break;          
        }

        case 'Remicade':
        {
            PatientRoadMapList = 
            [
                {
                    "linename" : "Initiate Prior Authorization",
                    "whereToNavigate" : "InsuranceCoverage"
                },
                {
                    "linename" : "Instant Benefits Investigation",
                    "whereToNavigate" : "InsuranceCoverage"
                },
                {
                    "linename" : "Check Eligibility and Enroll Patient into Savings Program",
                    "whereToNavigate" : "SavingsProgram"
                }
            ];
            break;          
        }

    return PatientRoadMapList;

Answer №1

To simplify your code, you can define a function to handle the expr value.

For example:

function myFunction(value) {
         var expr = value;
         ....
         // your code goes here
         ....
    }

Then, you can call this function wherever needed.

For instance:

alert(myFunction("Apples"));

or

var result = myFunction("strawberries");
function myFunction(value) {
  var expr = value;
    switch(expr) {
      case 'banana':
        return 'your banana';
         break;
      default:
        return 'not found';
     }
}
<html>
  <body>
  <div>
  <input value ="click" type="button" onclick =alert(myFunction('banana')); >
 </div>
 </body>
 </html>
 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to access variables with dynamic names in Vue.js

I'm curious if it's possible to dynamically access variables from Vue’s data collection by specifying the variable name through another variable. For instance, consider the following example: Here are some of the variables/properties: var sit ...

redirecting from an AJAX request

Seeking a way to perform a redirect following an ajax `put` request. My intention is to implement client-side validation using pure JS. Client: $(document).ready(function() { login = () => { var username = $("[name='username']"). ...

Keeping an Rxjs observable alive despite encountering errors by simply ignoring them

I am passing some values to an rxjs pipe and then subscribing to them. If there are any errors, I want to skip them and proceed with the remaining inputs. of('foo', 'bar', 'error', 'bazz', 'nar', 'erro ...

Error: Google Plus Cross-Origin Resource Sharing Issue

I recently developed an AngularJS application that utilizes Google's server-side flow for authentication. The issue arises when my AngularJS app is hosted on one server and the Rest API is hosted on another. After successfully logging in, I encounter ...

The appearance of the upload button in React using Material-UI seems off

After following the MUI document on React Button upload, I noticed that the UI results were different than expected. Instead of just showing the button UI, there was an additional UI element present. By adding the sx={{display:'none'}} property, ...

Determining the container height for every two-image row

I am currently working on a project for this website. My focus right now is on resizing vertical images using the following script: function Gallery(selector) { this.add_module = function (type, image) { var portrait_text = image.next('.portr ...

Displaying images dynamically in Angular using ng-repeat with URL strings

I'm working on a project where I need to dynamically call pictures inside an ng-repeat loop. Initially, I tried adding a key/value pair of 'img' to the JSON object I'm fetching and then dropping it inside the URL. However, this approach ...

Issue: Unable to assign headers once they have already been sent to the client

As a newcomer to the world of Node.js, I am facing some challenges. Currently, my setup includes Node.js 4.10 and Express 2.4.3. Whenever I attempt to access , I get redirected to . This redirection triggers the following error: Error: Headers cannot b ...

Is there a comparable Javascript alternative to the Ruby gem "site_prism" for Page Objects design?

Is there a JavaScript framework for Protractor in NodeJS that provides a clean way to define Page Object Elements similar to site_prism? I've explored astrolabe but it doesn't quite meet the requirements. This is an example of how Page Objects a ...

Trigger an animation function with JQuery once the first one has finished

I am attempting to create a step-by-step animation of a div in JQuery. Each animation is triggered by a click, followed by a double-click, and so on. My issue arises when the animations overlap. When I double-click the div, both the first and second anima ...

Where to Locate a String Excluding <a> Tags?

I'm in the process of writing a JavaScript code that will scan an HTML document and identify all occurrences of a specific keyword that are NOT contained within a link, meaning not under an <a> tag. To illustrate this, let's examine the fol ...

Having trouble accessing the initial two elements of an array while generating a Fibonacci series in JavaScript

Attempting to generate a Fibonacci sequence that always starts with [0, 1] using basic JS has proven challenging. The current implementation of the function does not properly return the first two items in an array when calling the corresponding n number. F ...

After reloading the data tables, analyze the information and dynamically highlight any rows in red that remain unchanged

I have a table that is refreshed every 5 minutes using ajax.reload(). One of the rows in this table is labeled as CIP. My goal is to highlight the CIP row on each refresh where the value remains unchanged from the previous value (value received in the la ...

Can cucumber steps be executed conditionally within a single scenario?

I would like to streamline my testing process by using one feature file for both desktop and mobile tests. I am looking to run two separate tests, with one tagged as @mobile and the other as @desktop. By doing this, I can avoid creating a duplicate feature ...

Exploring the Art of Navigation with Ionic and Angular

Trying to integrate Angular, Meteorjs, and Ionic Framework has been a smooth process, thanks to the urigo:angular and urigo:ionic packages. However, I'm facing difficulties in configuring ionic links and angular routing to make it work seamlessly. Des ...

Creating a dynamic hyperlink variable that updates based on user input

I am attempting to create a dynamic mailto: link that changes based on user input from a text field and button click. I have successfully achieved this without using the href attribute, but I am encountering issues when trying to set it with the href attr ...

Adding a blank data-columns attribute using jQuery:

I am trying to add the data-columns attribute for salvattore.js, but I am facing an issue where I cannot simply add the attribute without providing a value. Currently, my code looks like this: $('#liste-vdl div.view-content').attr("data-columns" ...

Issue with Element.update() method malfunctioning on IE browser is causing concern

Utilizing the Element.update() method from the prototype: [ ... ] var link_1 = new Element('a').update( '<' ); var link_2 = new Element('a').update( '<<' ); [ ... ] Encountering a unique issue only in IE: ...

What causes a never-ending loading cycle on a website when a 404 error occurs?

My Express.js/Node.js website is hosted on Heroku. I am facing an issue where the server does not properly send a 404 error when a file cannot be found. Instead, the page keeps loading endlessly. How can I make the server stop loading when it encounters a ...

How can I retrieve text instead of HTML using jQuery.get?

I'm facing an issue where I am trying to retrieve data from another site using the code below: jQuery.ajaxSetup({ crossDomain: true, dataType: "jsonp text" }); jQuery.get(url, function(rawContent) { console.log(rawContent); }); However, ...