You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何定义JavaScript函数动态切换产品获取linename与whereToNavigate

Dynamic Product Roadmap Function Fix

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:11:29