如何定义JavaScript函数动态切换产品获取linename与whereToNavigate
Hey there! Let's sort out this function so it properly returns the right roadmap configuration based on the product parameter you pass in. First, let's address the immediate issue in your original code.
The Problem in Your Current Code
You're overwriting the product parameter with var product = '#~Product~#'; right at the start of the function. This means no matter what value you pass into the function, it gets ignored entirely—so your dynamic switching never works as intended.
Fixed Version with Switch Statement
This version removes the parameter overwrite, adds a default case to handle unknown products, and uses modern scoping:
function getRoadmapList(product) { let 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; default: // Handle unknown products to avoid returning undefined patientRoadMapList = []; console.warn(`Unknown product requested: ${product}`); } return patientRoadMapList; }
More Elegant Version with Object Mapping
As you add more products, a switch statement can get unwieldy. A better approach is to use an object to map product names directly to their roadmap configs. This is cleaner and easier to maintain:
// Centralized map of products to their roadmap data const productRoadmaps = { 'Tremfya': [ { "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" } ], 'Remicade': [ { "linename": "Initiate Prior Authorization", "whereToNavigate": "InsuranceCoverage" }, { "linename": "Instant Benefits Investigation", "whereToNavigate": "InsuranceCoverage" }, { "linename": "Check Eligibility and Enroll Patient into Savings Program", "whereToNavigate": "SavingsProgram" } ] }; function getRoadmapList(product) { // Return the matching roadmap, or an empty array if the product isn't found return productRoadmaps[product] || []; }
How to Use It
Just call the function with your target product name:
// Get Tremfya's roadmap const tremfyaRoadmap = getRoadmapList('Tremfya'); // Get Remicade's roadmap const remicadeRoadmap = getRoadmapList('Remicade'); // Returns empty array for unknown products const unknownRoadmap = getRoadmapList('UnlistedProduct');
内容的提问来源于stack exchange,提问作者vineet

