【问题标题】:How to display matching description of an object nested in an array?如何显示嵌套在数组中的对象的匹配描述?
【发布时间】:2020-02-19 20:23:44
【问题描述】:

我有一个函数,其目的是显示嵌套在数组中的特定对象的特定描述。这个想法是,一旦函数 (findSettings()) 接收到特定的数组 (systemSettings) 和某个键 (tab12)对象,它应该贯穿 switch 语句并提供其匹配的描述。

换句话说,如果函数的参数是“tab12”,那么它应该返回描述“tab12 的描述”。

我尝试使用 find 方法找到匹配的对象,效果很好,但如果我尝试运行 switch 语句,它会返回错误:'Obejct is possible undefined'。

const systemSettings = [
  {key: 'tab1', value: 'Main Tab'}, 
  {key: 'tab12', value: 'Tab 12'}, 
  {key: 'tab13', value: 'Tab 13'}, 
  {key: 'tab4', value: 'Tab 4'}
]

type sampObj = {
  key: string;
  value: string;
}

let info: string = '';

function findSetting(arr: sampObj[], settingKey: string) {

  const selectedObjs = arr.find(obj => obj.key === settingKey);

  switch(selectedObjs.key) {
    case 'tab1':
      info += 'Description for tab1';
      break;
    case 'tab12':
      info += 'Description for tab12';
      break;
    case 'tab13':
      info += 'Description for tab13';
      break;
    case 'tab4':
      info += 'Description for tab4';
      break;
    default: 
      info += 'No description available'
  }

}

findSetting(systemSettings, 'tab12')```

Any assistance would be highly appreciated.
Thanks.

【问题讨论】:

    标签: javascript arrays typescript object switch-statement


    【解决方案1】:

    我认为更简单的解决方案是使用 Map 之类的东西来存储您对键的描述。此外,TypeScript 中有一个方便的实用程序类型,称为 Record,用于类型安全。那么您根本不必在 findSetting 函数中提供您的数组

      systemSettingsDescriptions: Record<string, string> = {
        tab1: "Description for tab1",
        tab2: "Description for tab2",
        tab3: "Description for tab3"
      };
    
      systemSettings: sampObj [] = [
        { key: "tab1", value: "Main Tab" },
        { key: "tab12", value: "Tab 12" },
        { key: "tab13", value: "Tab 13" },
        { key: "tab4", value: "Tab 4" }
      ];
    
      function findSetting(settingKey: string): string {
        this.systemSettingsDescriptions[settingKey] || "No description available";
      }
    

    【讨论】:

      【解决方案2】:

      如果具有指定键的对象不在数组中,const selectedObjs = arr.find(obj =&gt; obj.key === settingKey); 可以返回 undefined

      您可以在执行 switch 语句之前检查对象是否未定义:

      if(selectedObjs) {
         switch(selectedObjs.key) {
             case 'tab1':
                info += 'Description for tab1';
                break;
              case 'tab12':
                info += 'Description for tab12';
                break;
              case 'tab13':
                info += 'Description for tab13';
                break;
              case 'tab4':
                info += 'Description for tab4';
                break;
              default: 
                info += 'No description available'
        }
      }
      else {
         // Handle invalid settingKey case
      }
      

      【讨论】:

      • 添加if条件,继续打印错误:object is undefined.
      猜你喜欢
      • 1970-01-01
      • 2021-06-26
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 2022-11-05
      相关资源
      最近更新 更多